Case insensitive dictionaries and hashtables
I see a lot of this kind of thing when reviewing code.
Dictionary<string, SomeType> _dictionary = new Dictionary<string, SomeType>();
_dictionary.Add(key.ToLower(), someInstance);
// and elsewhere in the class
SomeType someInstance = _dictionary[key.ToLower()];
It's nice to see people worrying about case insensitivity (provided that's what you want) but there's an even better way to do it. You can specify a comparer in the constructor of the dictionary like so:
Dictionary<string, SomeType> _dictionary = new Dictionary<string, SomeType>(StringComparer.InvariantCultureIgnoreCase);
In this case we're using the case insensitive invariant culture string comparer provided for free on the StringComparer class.

Post By
Josh Twist
10:22
15 Nov 2005
» Next Post:
SQL Profiler with .NET
« Previous Post:
Josh Twist