You can initialize an array like this:
int [ ] arr = { 1, 2, 3, 4, 5 };
but List<T> doesn’t allow this:
List<int> list = { 1, 2, 3, 4, 5 };
What’s the reason behind this?
After all both allow this:
int [ ] arr = new int [ ] { 1, 2, 3, 4, 5 }; List<int> list = new List<int> { 1, 2, 3, 4, 5 };
Also why it’s not possible to do this with a LinkedList<T>?:
LinkedList<int> ll = new LinkedList<int>() { 1, 2, 3 };
Update
Thanks guys. Just saw the replies. I wanted to pick several answers but it didn’t let me so.
Why does the LinkedList has an Add method though explicit implementation? Will this likely be fixed? Because problems like this will just snowball into bigger ones when they are overlooked, right?
Your first sample is the standard language syntax for initializing an array of integers. The left-hand value evaluates to int[]. In the second sample you are attempting to assign an int[] to a List<int>. The assignment operator doesn’t support this as they are different types. A List<int> is not an array of type int. As you say, though, there is a constructor for List<int> that does take an int[] as an argument and the new syntactic sugar added in C# 3.0 allows you the convenience of using { } to add members to the collection defined by the default constructor.
As @Patrik says, this won’t work for LinkedList because it doesn’t define the Add() method as part of its interface (there is an explicit implementation of ICollection.Add) so the syntactic sugar won’t work.
There is an easy work-around for LinkedList, however.