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

C#List

C# Problem Overview


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"});

C# Solutions


Solution 1 - C#

Check out C# 3.0's Collection Initializers.

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

Solution 2 - C#

If you're looking to reduce clutter, consider

var lst = new List<string> { "foo", "bar" };

This uses two features of C# 3.0: type inference (the var keyword) and the collection initializer for lists.

Alternatively, if you can make do with an array, this is even shorter (by a small amount):

var arr = new [] { "foo", "bar" };

Solution 3 - C#

In C# 3, you can do:

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

This uses the new collection initializer syntax in C# 3.

In C# 2, I would just use your second option.

Solution 4 - C#

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

Solution 5 - C#

You can do that with

var list = new List<string>{ "foo", "bar" };

Here are some other common instantiations of other common Data Structures:

Dictionary

var dictionary = new Dictionary<string, string> 
{
    { "texas",   "TX" },
    { "utah",    "UT" },
    { "florida", "FL" }
};

Array list

var array = new string[] { "foo", "bar" };

Queue

var queque = new Queue<int>(new[] { 1, 2, 3 });

Stack

var queque = new Stack<int>(new[] { 1, 2, 3 });

As you can see for the majority of cases it is merely adding the values in curly braces, or instantiating a new array followed by curly braces and values.

Solution 6 - C#

You can drop the new string[] part:

List<string> values = new List<string> { "one", "two", "three" };

Solution 7 - C#

You can simplify that line of code slightly in C# by using a collection initialiser.

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

Solution 8 - C#

You can create helper generic, static method to create list:

internal static class List
{
    public static List<T> Of<T>(params T[] args)
    {
        return new List<T>(args);
    }
}

And then usage is very compact:

List.Of("test1", "test2", "test3")

Solution 9 - C#

You can just:

var list = new List<string> { "red", "green", "blue" };

or

List<string> list = new List<string> { "red", "green", "blue" };

Checkout: Object and Collection Initializers (C# Programming Guide)

Solution 10 - C#

If you want to create a typed list with values, here's the syntax.

Assuming a class of Student like

public class Student {
   public int StudentID { get; set; }
   public string StudentName { get; set; }
 }   

You can make a list like this:

IList<Student> studentList = new List<Student>() { 
                new Student(){ StudentID=1, StudentName="Bill"},
                new Student(){ StudentID=2, StudentName="Steve"},
                new Student(){ StudentID=3, StudentName="Ram"},
                new Student(){ StudentID=1, StudentName="Moin"}
            };

Solution 11 - C#

If we assume there is a class named Country , than we can do like this:

var countries = new List<Country>
{
    new Country { Id=1, Name="Germany", Code="De" },
    new Country { Id=2, Name="France",  Code="Fr" }
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPiotr CzaplaView Question on Stackoverflow
Solution 1 - C#Neil WilliamsView Answer on Stackoverflow
Solution 2 - C#Konrad RudolphView Answer on Stackoverflow
Solution 3 - C#Reed CopseyView Answer on Stackoverflow
Solution 4 - C#Matt GrandeView Answer on Stackoverflow
Solution 5 - C#Braden BrownView Answer on Stackoverflow
Solution 6 - C#John RaschView Answer on Stackoverflow
Solution 7 - C#NoldorinView Answer on Stackoverflow
Solution 8 - C#Konrad KokosaView Answer on Stackoverflow
Solution 9 - C#MantchovaView Answer on Stackoverflow
Solution 10 - C#FoxDeployView Answer on Stackoverflow
Solution 11 - C#nzrytmnView Answer on Stackoverflow