While you can initialize a dictionary with collection initializers, this is pretty cumbersome. Especially for something that should be syntactic sugar.
Dictionary initializers are much cleaner:
var myDict = new Dictionary<int, string> { [1] = "Pankaj", [2] = "Pankaj", [3] = "Pankaj" };
Moreover, these initializers are not only for dictionaries, they can be used for any object that supports 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
i3arnon Dec 02 '14 at 7:47 2014-12-02 07:47
source share