How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)
168,850
Solution 1
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
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
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 4
Just lose the parenthesis:
var nameslist = new List<string> { "one", "two", "three" };
Solution 5
Remove the parentheses:
List<string> nameslist = new List<string> {"one", "two", "three"};
Author by
Johannes
Updated on February 23, 2021Comments
-
Johannes about 3 years
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"?
-
SilverbackNet over 13 yearsI like this no-parentheses method, what c# version did that start with?
-
Richard Fawcett over 13 yearsOops, look like five people beat me to it.
-
Jon Skeet over 13 yearsIt'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, withlist = tmp;
at the end. This can be important if you're reassigning a variable's value. -
Matthew Abbott over 13 yearsAutomatic 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 over 13 years@Jon, Cheers, I never knew that part. :D
-
Tony almost 9 years@Jon Skeet : "...This can be important if you're reassigning a variable's value." can you explain a bit more on this remark?
-
Tony almost 9 yearswhat is the purpose of using the second line " List<string> nameslist = new List<string>(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 [] ???
-
NetMage about 4 years@Tony Consider:
list = new List<string> { list[1], list[2], list[0] };
- you don't wantlist
replaced with an emptyList<string>
before the elements are added.