While working on creating an application framework for a personal project, I needed a way to expose a Dictionary<TKey, TValue> object from one of my methods. The only caveat being, I wanted the Dictionary object to be read-only.

I knew that there exists a ReadOnlyCollection<T> in the FCL, but to my dismay there is no built-n way of creating a read-only Dictionary in .NET.  I searched the web to see how other people are solving this problem, but didn’t find a suitable implementation that I was happy with.  Hence I created a nice little ReadOnlyDictionary<TKey, TValue> that essentially allows developers to expose an immutable Dictionary object of specified key and value types.

As you can see the class itself is very concise. This is because it cleverly derives from ReadOnlyCollection<KeyValuePair<TKey, TValue>>, and only adds two methods and an indexer.  Also it uses LINQ to Objects to select Dictionary items, and the LINQ query is neatly tucked in a private method where it’s re-used from other public methods.

Post a Comment

Be the First to Comment!

wpDiscuz