i7i27g
Last Updated: February 25, 2016
·
1.304K
· ChrisMissal
3c4478e6ae6c60b73d21c9fa0d1785ea

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.

Say Thanks
Respond

2 Responses
Add your response

1098
Ea0ea06d4e10f2f2c7171129d67939f7

:heart:

over 1 year ago ·
3684
E519e735596b76d539a193bfabff1d81

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

over 1 year ago ·