Bhavya

Code -> Break -> Fix -> Blog

C# 6.0 : Easy Dictionary Initializer

1 Comment

Below is the code to initialize a Dictionary object for C# version < 6.0:

Dictionary<int, string> employees = new Dictionary<int, string> {
{1, "Gabbar"},
{2, "Langda Tyagi"},
{3, "Mogambo"},
{4, "Kesariya Vilayt"},
{5, "Lotiya Pathan"}
};

But with C# 6.0 the above code can be made simple,

Dictionary<int, string> employees = new Dictionary<int, string>;
{
[1] = "Gabbar",
[2] = "Langda Tyagi",
[3] = "Mogambo",
[4] = "Kesariya Vilayt",
[5] = "Lotiya Pathan"
};

Let me know your thoughts.

~BS

Advertisements

One thought on “C# 6.0 : Easy Dictionary Initializer

  1. Pingback: New Features of C# 6.0 | Bhavya

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s