#1,215 – C# 6.0 – New Syntax for Dictionary Initializers
October 30, 2014 6 Comments
Prior to C# 6.0, you could initialize a Dictionary object with a collection initializer as follows:
// Initialization with collection initializer Dictionary<string, int> guys = new Dictionary<string, int> { {"Galileo", 1564}, {"Magellan", 1480}, {"Voltaire", 1694}, {"Kepler", 1571}, {"Keaton", 1895} };
In C# 6.0, the following syntax will also work:
// Initialization with new dictionary initializer Dictionary<string, int> guys = new Dictionary<string, int> { ["Galileo"] = 1564, ["Magellan"] = 1480, ["Voltaire"] = 1694, ["Kepler"] = 1571, ["Keaton"] = 1895 };
Note that as of 29 Oct, 2014, this feature is not enabled in the current Visual Studio 2014 CTP.