Implicitly initialized dictionaries

Dictionaries can be implicitly initialized just like lists:

Dictionary<int, string> map = new Dictionary<int, string>()
{
    { 1, "one" },
    { 2, "two" }
};

Can be useful when you have a static map of values, for example.. It can be initialized in a static constructor, of course, but this can be cleaner.

For whatever reason VS doesn’t autoformat it, btw.

Leave a Reply

Your email address will not be published. Required fields are marked *