Initialize a Dictionary with index initializers
The next release of C# 6 has some amazing new features. In a series of blog posts I will cover some of them.
- Chained null checks
- The nameof operator
- Awesome string formatting
- Expression-bodied methods
- Auto-property initializers
- Initialize a Dictionary with index initializers (this one)
A small but still significant feature in C# 6 is index initializers. Index initializers can be sued to initialize object members, but also dictionaries. Initializing a dictionary has always be cumbersome, but not anymore.
var numbers = new Dictionary<int, string>
{
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
There are other great new features in C# that I have not touched – have a look at the blog post New features in C# 6 by Mads Torgersen, Principal Program Manager, VS Managed Languages.
Comments