141

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"?

John Smith
  • 7,243
  • 6
  • 49
  • 61
Johannes
  • 6,490
  • 10
  • 59
  • 108

9 Answers9

221
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");
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • 1
    I like this no-parentheses method, what c# version did that start with? – SilverbackNet Dec 14 '10 at 10:36
  • 10
    It's not *quite* translated into that, at least not in general. The assignment to the variable happens *after* all the `Add` calls have been made - it's as if it uses a temporary variable, with `list = tmp;` at the end. This can be important if you're *reassigning* a variable's value. – Jon Skeet Dec 14 '10 at 10:38
  • Automatic Properties and Object Initialisers were introduced with .NET 3 I believe, it's the same Framework version, just an updated compiler to support LINQ. – Matthew Abbott Dec 14 '10 at 10:38
  • @Jon, Cheers, I never knew that part. :D – Matthew Abbott Dec 14 '10 at 10:39
  • @Jon Skeet : "...This can be important if you're reassigning a variable's value." can you explain a bit more on this remark? – Tony May 24 '15 at 02:48
  • 1
    @Tony Consider: `list = new List { list[1], list[2], list[0] };` - you don't want `list` replaced with an empty `List` before the elements are added. – NetMage Apr 01 '20 at 21:31
22

Change the code to

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

or

List<string> nameslist = new List<string>(new[] {"one", "two", "three"});
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • 1
    what is the purpose of using the second line " List nameslist = new List(new[] {"one", "two", "three"}); " when can we use it ? Also what is meaning of "new[] {...} " in the second syntax ?why new keyword is used along with the parenthesis [] ??? – Tony May 24 '15 at 02:52
7

Just lose the parenthesis:

var nameslist = new List<string> { "one", "two", "three" };
Joe the Person
  • 4,470
  • 3
  • 22
  • 32
Richard Fawcett
  • 2,799
  • 1
  • 29
  • 36
7

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


        
MDT
  • 1,535
  • 3
  • 16
  • 29
4

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" };
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
3
List<string> nameslist = new List<string> {"one", "two", "three"} ?
Romain Meresse
  • 3,044
  • 25
  • 29
3

Remove the parentheses:

List<string> nameslist = new List<string> {"one", "two", "three"};
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
3

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" };
Sam Salisbury
  • 1,066
  • 11
  • 19
3

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" };
Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37