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 as earlier.

 

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

In C# 6.0, the dictionary initialization happens 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.

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

12 comments

  1. "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 [0] = “Ex000001”."

    Do you mean to say that assigning to item 1 is retrieved via item 0? That seems like a recipe for disaster.

    ReplyDelete
    Replies
    1. lmfao. I was just thinking the same thing. Seems like that is a positional counter and not dictionary key.

      Delete
    2. Sorry, my mistake. In the said exam ple, it's not errorCodes [0] = “Ex000001" but errorCodes [1] = “Ex000001". The value "1" is actually the key of the dictionary.

      Delete
  2. @Clay + Mark: The key 1 is arbitrary. It could've been for instance "foo". Then you would retrieve the value as such: errorCodes["foo"]. What you are talking about is the index.

    ReplyDelete
  3. IM(no so)HO this is a weird feature. Instead of using the old ways, we use the OLD ways but with brackets?! Where is the improvement? I might be missing the cool point here so I'm not ranting (yet). Someone care to explain? Because if not, I have a "cool" feature too. Instead of typing int a = 3; we can now go int a = (int)3; but what's the point?

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Nice article Kunal. Also looking at the comment by @TLRonin this would also be valid code.

    public static string Ex000001 { get; set; } = "Error description 1";
    public static string Ex000002 { get; set; } = "Error description 2";
    public static string Ex000003 { get; set; } = "Error description 3";
    public static string Ex000004 { get; set; } = "Error description 4";
    public static string Ex000005 { get; set; } = "Error description 5";
    public static string Ex000006 { get; set; } = "Error description 6";
    public static string Ex000007 { get; set; } = "Error description 7";

    public static void GetErrorCodeDescription()
    {
    Dictionary ErrorDesc = new Dictionary
    {
    [Ex000001] = Ex000001 + " : Additional Error Information for error description 1",
    [Ex000002] = Ex000002 + " : Additional Error Information for error description 2",
    [Ex000003] = Ex000003 + " : Additional Error Information for error description 3",
    [Ex000004] = Ex000004 + " : Additional Error Information for error description 4",
    [Ex000005] = Ex000005 + " : Additional Error Information for error description 5",
    [Ex000006] = Ex000006 + " : Additional Error Information for error description 6",
    [Ex000007] = Ex000007 + " : Additional Error Information for error description 7"
    };

    Console.WriteLine(ErrorDesc[Ex000005]);
    }

    ReplyDelete
    Replies
    1. Yes Dirk, and this will print: "Error description 5 : Additional Error Information for error description 5".

      Delete
  6. Also another point to remember, am I correct when saying that this (and certain other features) of C# 6 is supported on earlier frameworks such as .NET 4.5?

    ReplyDelete
    Replies
    1. I don't think, you will be allowed to use C# 6.0 with .NET 4.5.

      Delete
    2. I haven't tested it, but I don't see any reason why this shouldn't be possible. None of the new C# features seem to require any new libraries or platform features, and so the produced assemblies should be compatible with prior .NET versions back to 2.0 (except await which requires 4.0 or 4.5).

      Delete
    3. @Kunal and @Dirk, the new dictionary initializer is only a compile-time feature. You can still produce applications that target earlier framework versions (tested with 4.5.2).

      Delete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top