Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefits does dictionary initializers add over collection initializers?

In a recent past there has been a lot of talk about whats new in C# 6.0
One of the most talked about feature is using Dictionary initializers in C# 6.0
But wait we have been using collection initializers to initialize the collections and can very well initialize a Dictionary also in .NET 4.0 and .NET 4.5 (Don't know about old version) like

Dictionary<int, string> myDict = new Dictionary<int, string>() {     { 1,"Pankaj"},     { 2,"Pankaj"},     { 3,"Pankaj"} }; 

So what is there new in C# 6.0, What Dictionary Initializer they are talking about in C# 6.0

like image 945
Pankaj Avatar asked Dec 02 '14 07:12

Pankaj


People also ask

How to initialize values in dictionary in C#?

One way to initialize a Dictionary<TKey,TValue>, or any collection whose Add method takes multiple parameters, is to enclose each set of parameters in braces as shown in the following example. Another option is to use an index initializer, also shown in the following example.

What is collection initializers in C#?

Collection initializers let you specify one or more element initializers when you initialize a collection type that implements IEnumerable and has Add with the appropriate signature as an instance method or an extension method. The element initializers can be a simple value, an expression, or an object initializer.

How do you initialize a new dictionary?

Dictionaries are also initialized using the curly braces {} , and the key-value pairs are declared using the key:value syntax. You can also initialize an empty dictionary by using the in-built dict function. Empty dictionaries can also be initialized by simply using empty curly braces.


2 Answers

While you could initialize a dictionary with collection initializers, it's quite cumbersome. Especially for something that's supposed to be syntactic sugar.

Dictionary initializers are much cleaner:

var myDict = new Dictionary<int, string> {     [1] = "Pankaj",     [2] = "Pankaj",     [3] = "Pankaj" }; 

More importantly these initializers aren't just for dictionaries, they can be used for any object supporting an indexer, for example List<T>:

var array = new[] { 1, 2, 3 }; var list = new List<int>(array) { [1] = 5 }; foreach (var item in list) {     Console.WriteLine(item); } 

Output:

1 5 3 
like image 153
i3arnon Avatar answered Sep 23 '22 02:09

i3arnon


Just to stress the most important difference, dictionary initializer calls the indexer, and hence it performs an update when duplicate keys are encountered, whereas collection initializer calls the Add method which will throw.

To briefly summarize the differences in general:

  1. Collection initializer calls Add method (for IEnumerables) where as dictionary initializer calls indexer. This has the Add vs Update semantic differences for dictionaries.

  2. Dictionary initializer is technically an object initializer, hence can be mixed with initializing other properties. For e.g.:

    new Dictionary<int, string> {     [1] = "Pankaj",     [2] = "Pankaj",     [3] = "Pankaj",     Capacity = 100, }; 

    but not

    new Dictionary<int, string>()  {     { 1,"Pankaj" },     { 2,"Pankaj" },     { 3,"Pankaj" },     Capacity = 100, // wont compile }; 
  3. Being just an object initializer, indexed initializer can be used for any class with an indexer, whereas collection initializer can be used only for IEnumerables, which should be obvious anyway.

  4. Collection initializer can be enhanced with custom Add extension methods, whereas ditionary initializer can't be (no extension indexer in C# yet).

  5. Dictionary initializer maybe subjectively slightly more readable when it comes to initializing a dictionary :)

  6. Dictionary initializer is C# 6.0 feature whereas collection initializer is available from C# 3.0 onwards.

like image 34
nawfal Avatar answered Sep 20 '22 02:09

nawfal