What Was I Thinking?

Follies & Foils of .NET Development

  Home  |   Contact  |   Syndication    |   Login
  28 Posts | 0 Stories | 52 Comments | 0 Trackbacks

News

Archives

Post Categories

Check These Out

Gurus

I've often thought about making my own implementation of a Dictionary<K,V> where the key values are case insensitive.

Its been one of those things on my ever-growing to-do list. I usually end up casting the key values to all uppercase and try to encapsulate all my calls the the dictionary with my own logic that performs the case conversion.  A case insensitive dictionary would avoid all that nastiness.

Sometimes it's helpful to read the tooltip overloads. As I coded a new dictionary instance today, I stumbled upon the

Dictionary<(Of <(TKey, TValue>)>) Constructor (IEqualityComparer<(Of <(TKey>)>)) overload.  

Apparently I can pass my own equality comparer into the constructor which tells the dictionary how to compare key values.   Could it really be that easy?

Yep, it exactly that easy.  To make my dictionary<string,string> use case insensitive keys, declare the dictionary as follows:

Dictionary<string, string> argList = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);

Now  args["Test"] and args["TEST"] return the same item from my dictionary.

I love it when I can mark things done on my to-do list without actually having to do any work.

posted on Wednesday, April 30, 2008 3:28 PM