Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Dictionary Initializer


Dictionary Initializer or Index Initializers:

Developer feels with C# 5.0 or previously syntax for instantiating dictionaries is complex and confusing.

With C# 6.0 the syntax for creating dictionaries is now more simple and clear. It makes the code easier to read.


C# 5.0 (VS 2012)

  Dictionary<string,string> StudentNames = new Dictionary<string, string>()
{
    { "Nagnath Kendre", "Engineering" },
    { "Sharad Sangle", "arts" },
    { "Girish Sarode", "Commerce" }
};


C# 6.0 (VS 2015)

  Dictionary<string,string> StudentNames = new Dictionary<string, string>()
{
    ["Nagnath Kendre"] = "Engineering",
    ["Sharad Sangle"]= "arts" ,
    ["Girish Sarode"] = "Commerce"
};

 < 

Invalid entry,please enter valid data.

Loading