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

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:

Just lose the parenthesis:

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

Solution 4:

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