Last Updated: October 16, 2018
· ChrisMissal

Case Insensitive Dictionaries in .Net

If you're using a Dictionary (with string keys) in .Net and you ever need to call .ToLower() or .ToUpper() on your key, remember that you can create that dictionary with irrelevant casing:

new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

I see this most useful when a string is the key, but all you really need here is an instance of IEqualityComparer<T>, where T is your key's type. In the example above, I'm just leveraging something built into the framework already.

2 Responses
Add your response



over 1 year ago ·

Nice, thanks. Also, System.Collections.Specialized.StringDictionary is always case-insensitive, and actually keys are lowered before hashing.

over 1 year ago ·