29

I've just noticed that when you declare a List in c# you can put parentheses or curly braces at the end.

List<string> myList = new List<string>();
List<string> myList2 = new List<string>{};

Both these list appear to have the same functionality. Is there any actual difference caused by declaring them with parentheses or curly braces?

senshin
  • 10,022
  • 7
  • 46
  • 59
MikeS159
  • 1,884
  • 3
  • 29
  • 54
  • 1
    In addition to all answers, and to be clear, note that both lines will compile to the same IL, so there is really no functional difference. Use the first version when you don't need the collection initializer (curly braces). – Patrice Gahide Sep 17 '16 at 15:46
  • @PatriceGahide, good to know that it will compile the same either way. I asked in case there was some subtle difference that might cause issues one day. – MikeS159 Sep 18 '16 at 00:45

4 Answers4

45

The use of curly braces { } is called a collection initializer. For types that implement IEnumerable the Add method would be invoked normally, on your behalf:

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

Empty collection initializers are allowed:

List<string> myList2 = new List<string>() { };

And, when implementing an initializer, you may omit the parenthesis () for the default constructor:

List<string> myList2 = new List<string> { };

You can do something similar for class properties, but then it's called an object initializer.

var person = new Person
                 {
                     Name = "Alice",
                     Age = 25
                 };

And its possible to combine these:

var people = new List<Person>
                 {
                     new Person
                         {
                             Name = "Alice",
                             Age = 25
                         },
                     new Person
                         {
                             Name = "Bob"
                         }
                 };

This language feature introduced in C# 3.0 also supports initializing anonymous types, which is especially useful in LINQ query expressions:

var person = new { Name = "Alice" };

They also work with arrays, but you can further omit the type which is inferred from the first element:

var myArray = new [] { "one", "two", "three" };

And initializing multi-dimensional arrays goes something like this:

var myArray = new string [,] { { "a1", "b1" }, { "a2", "b2" }, ... };

Update

Since C# 6.0, you can also use an index initializer. Here's an example of that:

var myDictionary = new Dictionary<string, int>
                       {
                           ["one"] = 1,
                           ["two"] = 2,
                           ["three"] = 3
                       };
Biscuits
  • 1,767
  • 1
  • 14
  • 22
7

They have different semantics.

List<string> myList = new List<string>();

The above line initializes a new List of Strings, and the () is part of the syntax of building a new object by calling its constructor with no parameters.

List<string> myList2 = new List<string>{};

The above line initializes a new List of Strings with the elements presented inside the {}. So, if you did List<string> myList2 = new List<string>{"elem1", "elem2"}; you are defining a new list with 2 elements. As you defined no elements inside the {}, it will create an empty list.

But why does the second line have no () ?

That makes part of a discussion in which omitting the parenthesis in this case represents a call to the default constructor. Take a look at This Link

Community
  • 1
  • 1
rafaelc
  • 57,686
  • 15
  • 58
  • 82
5

The first version initialises an empty list. The second version is used to initialise the list with values. You wouldn't, or shouldn't, see the second version used without at least one instance of T.

So you could do something like:

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

or

List<T> Foo = new List<T>{SomeInstancesOfT};

This is useful in many places when initialising objects.

See https://msdn.microsoft.com/en-us/library/bb384062.aspx

Steve
  • 2,950
  • 3
  • 21
  • 32
1

To be short - there is no difference in the objects created.

But there's more: What you are asking isn't a List specific question - it's a question of object and Collection initialization.

See here.

steamrolla
  • 2,373
  • 1
  • 29
  • 39
  • 4
    Why not summarise the explanation at the end of that link in your answer? Saves everyone a click, avoids the issue of link rot and makes your answer more useful. – Basic Sep 17 '16 at 15:36