C# 7.0 is on the horizon but I’m still getting used to some of the new features of C# 6.0. One of the features I didn’t use until recently were dictionary initializers.
Before C# 6.0, you had to do something like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var codes=new Dictionary<int,string> | |
{ | |
{1000,"Brussels"}, | |
{2000, "Antwerp"}, | |
{9000,"Ghent"} | |
} |
In C# 6.0, you could separate keys from values by using the indexer syntax making the code easier to read and understand:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var codes=new Dictionary<int,string> | |
{ | |
[1000]="Brussels", | |
[2000]="Antwerp", | |
[9000]="Ghent", | |
} |