With C# 6.0 we get dictionary initialization using indexers which is yet another subtle feature towards cleaner and more understandable code. The following is an example showing how you would usually initialize a dictionary.

var dictionary = new Dictionary<string, FilterDelegate> { 
    {"0x4EA8", () => Console.WriteLine("Hello")}, {"0x2AF3", () => Console.WriteLine("World")} 
};

This looks fairly peaceful however when thinking about it, the above example is nothing more than a key-value collection yet its syntax does not represent this very well.

Below is another example from MSDN to better display this unreadability and to better help imagine how it could be difficult to read.

var students = new Dictionary<int, StudentName>() { 
    { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}}, 
    { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}}, 
    { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}} 
};

The syntax could generally be improved to better display a key-value and with the new version of C# it will be.

C# 6.0 – Dictionary initializers

Here is a rewrite of the first example with the new index member initialization.

var dictionary = new Dictionary<string, FilterDelegate> { 
    ["0x4EA8"] = () => Console.WriteLine("Hello"), 
    ["0x2AF3"] = () => Console.WriteLine("World") 
};

and the same for the second example

var students = new Dictionary<int, StudentName>() { 
    [111] = new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}, 
    // and so on.. 
};

It’s simply simpler without all the curly braces!

That’s it for now!