Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection initialization

Tags:

c#

.net

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?

like image 205
Joan Venge Avatar asked Jan 05 '09 21:01

Joan Venge


People also ask

What is collection initializer?

C# - Object Initializer Syntax NET 3.5) introduced Object Initializer Syntax, a new way to initialize an object of a class or collection. Object initializers allow you to assign values to the fields or properties at the time of creating an object without invoking a constructor.

What is initialize in C#?

Initialization sets the variable to a new instance. It must be to a type that is compatible with the declaration type. 1static void Main(string[] args) 2{ 3 string a = "Hello World"; 4 Console.

What is object initialization why it is required?

In object initializer, you can initialize the value to the fields or properties of a class at the time of creating an object without calling a constructor. In this syntax, you can create an object and then this syntax initializes the freshly created object with its properties, to the variable in the assignment.

What is object initializer?

An object initializer is an expression that describes the initialization of an Object . Objects consist of properties, which are used to describe an object. The values of object properties can either contain primitive data types or other objects.


1 Answers

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.

public class LinkedListWithInit<T> : LinkedList<T>
{
    public void Add( T item )
    {
        ((ICollection<T>)this).Add(item);
    }
}

LinkedList<int> list = new LinkedListWithInit<int> { 1, 2, 3, 4, 5 };
like image 97
tvanfosson Avatar answered Nov 15 '22 16:11

tvanfosson