Search code examples
c#list

Quick way to create a list of values in C#?


I'm looking for a quick way to create a list of values in C#. In Java I frequently use the snippet below:

List<String> l = Arrays.asList("test1","test2","test3");

Is there any equivalent in C# apart from the obvious one below?

IList<string> l = new List<string>(new string[] {"test1","test2","test3"});

Solution

  • Check out C# 3.0's Collection Initializers.

    var list = new List<string> { "test1", "test2", "test3" };