- Posted by liammclennan on November 18, 2009
Dictionary<TKey, TValue> is a generic type that stores collections of KeyValuePair<TKey, TValue>. It is used heavily (actually the IDictionary<TKey, TValue> interface) in Asp.Net Mvc as a parameter to view helper methods.
I am writing this post because I have a tendency to forget the collection initializer syntax for this type of collection, so here it is:
IDictionary<string, int> collection = new Dictionary<string, int>()
{
{ "rows", 7 },
{ "columns", 2}
}
The nice part is that it supports specifying the collection members as anonymous types, instead of having to instantiate new instances of KeyValuePair<TKey, TValue>.