Sep 28, 2015

New way of initializing a dictionary in C# 6.0

As I have already mention new feature list of C# 6.0 in my previous blog.

Today I am going to explore and talk about initializing a dictionary in a new way.

1. Old way to initializing a dictionary

Dictionary oldWayDict = new Dictionary()
{
    { "Name", "Kalpesh" },
    { "Surname", "Satasiya" },
    { "City", "Ahmedabad" }
};


2. New way to initializing a dictionary 

Dictionary newWayDict = new Dictionary()
{  
    ["Name"] = "Kalpesh",  //Initializing like string indexer
    ["Surname"] = "Satasiya",
    ["City"] = "Ahmedabad"
};


Here you can look at difference between old way and new way of initializing a dictionary.

Hope you like new C# features. Keep reading....

No comments:

Post a Comment