Before C# 6, we could initialize a Dictionary using the { key, value } syntax:
1
2
3
4
5
| var dictionary = new Dictionary<string, string>{ { "LAX", "Los Angeles International Airport" }, { "MEL", "Melbourne Tullamarine Airport" }}; |
While this syntax works fine, if you’re initializing complex objects with nested structures, all these curly braces get in the way and you may easily lose track of them.
C# 6 introduces a new index-like syntax to initialize dictionaries:
1
2
3
4
5
| var dictionary = new Dictionary<string, string>{ ["LAX"] = "Los Angeles International Airport", ["MEL"] = "Melbourne Tullamarine Airport"}; |
With this syntax, the extra noise due to curly braces is gone. Plus, keys stand out a bit more.
0 comments:
Post a Comment