Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#

What’s New in C# 6.0? - Dictionary Initializer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Jan 2015CPOL2 min read 36.4K   9   4
What’s New in C# 6.0? - Dictionary Initializer

Visual Studio 2015 and .NET 4.6 came up with another new feature (more precisely an enhancement) for C# 6.0 named “Dictionary Initializer”. It’s not a new thing but now you will be able to map the key/indexer directly at the time of dictionary object initialization.

Today in this blog post, we will discuss about this with an example. Don’t forget to read the other new features which I posted earlier. Links can be found in this post.

Don’t forget to read my previous posts on this series:

Dictionary Initialization is not a new thing in C#. It was present since C# 3.0, but in C# 6.0 Microsoft enhanced this by adding a new ability to use the key/indexer to map with the dictionary values directly during the initialization itself.

The following code snippet shows how dictionary initializer were used prior to C# 6.0 to initialize dictionary objects with key and values. This is still valid and you can use it as earlier.

Whats new in CSharp 6.0 - Dictionary Initialization (old ways)

In C# 6.0, the dictionary initialization happens in the same way but with keys as indexers which makes the code easy to read and understandable. Here is a piece of code to show you how the new initializing feature can be used. This is almost similar to how you access them e.g. errorCodes [1] = “Ex000001”.

Whats new in CSharp 6.0 - Dictionary Initialization (new way)

How much did you like this new feature/enhancement in C# 6.0 and how much will you use it in coming projects when you start using C# 6.0? Do let us know in the below comment section. Don’t forget to read the other posts on the same topic “What’s new in C# 6.0?”

Subscribe to my blog’s RSS feed and Email Newsletter to get the immediate updates directly delivered to your inbox. I am available on Twitter, Facebook, Google+ and LinkedIn. Do connect with me there and get the updates that I share over those social networking sites.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralSupport for indexers Pin
oliwan9-Sep-17 6:44
oliwan9-Sep-17 6:44 
GeneralRe: Support for indexers Pin
Kunal Chowdhury «IN»12-Sep-17 2:26
professionalKunal Chowdhury «IN»12-Sep-17 2:26 
Arrays, Lists etc. already supported it in previous versions, but in C# 6.0, it got the support for Dictionary too.
Vote up or Mark as Answered, if this information helped you.
Kind Regards - Kunal Chowdhury | Microsoft MVP (Windows Development), Windows 10 Champion
Checkout my 'Technical blog':     http://www.kunal-chowdhury.com


QuestionC# 6.0 - Dictionary Initializer Pin
Pradeep Mohanta 473331520-Jan-15 18:34
professionalPradeep Mohanta 473331520-Jan-15 18:34 
Questionquestion Pin
pop ioana20-Jan-15 6:56
pop ioana20-Jan-15 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.