How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)

C#ListCollectionsInitialization

C# Problem Overview


I am writing my testcode and I do not want wo write:

List<string> nameslist = new List<string>();
nameslist.Add("one");
nameslist.Add("two");
nameslist.Add("three");

I would love to write

List<string> nameslist = new List<string>({"one", "two", "three"});

However {"one", "two", "three"} is not an "IEnumerable string Collection". How can I initialise this in one line using the IEnumerable string Collection"?

C# Solutions


Solution 1 - C#

var list = new List<string> { "One", "Two", "Three" };

Essentially the syntax is:

new List<Type> { Instance1, Instance2, Instance3 };

Which is translated by the compiler as

List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");

Solution 2 - C#

Change the code to

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

or

List<string> nameslist = new List<string>(new[] {"one", "two", "three"});

Solution 3 - C#

Just lose the parenthesis:

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

Solution 4 - C#

Posting this answer for folks wanting to initialize list with POCOs and also coz this is the first thing that pops up in search but all answers only for list of type string.

You can do this two ways one is directly setting the property by setter assignment or much cleaner by creating a constructor that takes in params and sets the properties.

class MObject {        
        public int Code { get; set; }
        public string Org { get; set; }
    }

List<MObject> theList = new List<MObject> { new MObject{ PASCode = 111, Org="Oracle" }, new MObject{ PASCode = 444, Org="MS"} };

OR by parameterized constructor

class MObject {
        public MObject(int code, string org)
        {
            Code = code;
            Org = org;
        }

        public int Code { get; set; }
        public string Org { get; set; }
    }

List<MObject> theList = new List<MObject> {new MObject( 111, "Oracle" ), new MObject(222,"SAP")};


        

Solution 5 - C#

List<string> nameslist = new List<string> {"one", "two", "three"} ?

Solution 6 - C#

Remove the parentheses:

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

Solution 7 - C#

It depends which version of C# you're using, from version 3.0 onwards you can use...

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

Solution 8 - C#

I think this will work for int, long and string values.

List<int> list = new List<int>(new int[]{ 2, 3, 7 });


var animals = new List<string>() { "bird", "dog" };

Solution 9 - C#

This is one way.

List<int> list = new List<int>{ 1, 2, 3, 4, 5 };

This is another way.

List<int> list2 = new List<int>();

list2.Add(1);

list2.Add(2);

Same goes with strings.

Eg:

List<string> list3 = new List<string> { "Hello", "World" };

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
QuestionJohannesView Question on Stackoverflow
Solution 1 - C#Matthew AbbottView Answer on Stackoverflow
Solution 2 - C#Adriaan StanderView Answer on Stackoverflow
Solution 3 - C#Richard FawcettView Answer on Stackoverflow
Solution 4 - C#MDTView Answer on Stackoverflow
Solution 5 - C#Romain MeresseView Answer on Stackoverflow
Solution 6 - C#Tim RobinsonView Answer on Stackoverflow
Solution 7 - C#Sam SalisburyView Answer on Stackoverflow
Solution 8 - C#Muhammad AwaisView Answer on Stackoverflow
Solution 9 - C#Minhajuddin KhajaView Answer on Stackoverflow