Geeks With Blogs
Liam McLennan hackingon.net

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>.

Posted on Tuesday, November 17, 2009 9:02 PM | Back to top

Copyright © Liam McLennan | Powered by: GeeksWithBlogs.net