Home
C#
Dictionary Case-Insensitive
Updated Jan 10, 2024
Dot Net Perls
Case, Dictionary. A case-insensitive Dictionary is ideal in some programs. It helps with comparing file names in Windows, which ignore case.
Dictionary
Sometimes user names are case-insensitive—upper and lower letters are treated as equal. We implement a case-insensitive string Dictionary.
Requirements. Suppose we want to add a string "cat" to a Dictionary. But we want to be able to look up "CAT" and "Cat" also. The insensitive Dictionary is a solution.
cat -> cat, CAT, Cat
Example. The Dictionary class has several overloaded constructors. Here we use one that accepts a generic IEqualityComparer parameter.
Info IEqualityComparer is an interface. It is used by Dictionary to acquire hash codes and compare keys.
IEqualityComparer
Next We use a Dictionary constructor that accepts an IEqualityComparer. We specify StringComparer OrdinalIgnoreCase as the comparer.
Note StringComparer OrdinalIgnoreCase implements (at minimum) 2 methods, Equals and GetHashCode.
StringComparison
Note 2 These 2 methods are used internally by the Dictionary to compute hash keys and compare buckets.
using System; using System.Collections.Generic; // Create case insensitive string Dictionary. var test = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); test.Add("Cat", 2); test.Add("Python", 4); test.Add("DOG", 6); // Write value of "cat". Console.WriteLine("{0} = {1}", test.GetValueOrDefault("cat"), test.GetValueOrDefault("CAT")); // Write value of "PYTHON". Console.WriteLine("{0} = {1}", test.GetValueOrDefault("PYTHON"), test.GetValueOrDefault("Python")); // See if "dog" exists. if (test.ContainsKey("dog")) { Console.WriteLine("Contains dog."); } // Enumerate all KeyValuePairs. foreach (var pair in test) { Console.WriteLine(pair); }
2 = 2 4 = 4 Contains dog. [Cat, 2] [Python, 4] [DOG, 6]
Usage notes. You can add keys with either case using the Add method. The keys retain their original case internally in the Dictionary buckets.
And When you enumerate the KeyValuePairs (in foreach), the original cases are retained.
foreach
Warning When you add the strings "Cat" and "CAT", you will get an exception. The Dictionary considers the 2 strings to be equivalent.
ContainsKey. This method (along with TryGetValue) will accept keys of either case. So "PYTHON" will return the same value as "Python".
Dictionary ContainsKey
Dictionary TryGetValue
ToLower. To normalize string data in a Dictionary, you can call ToLower when adding or accessing keys. But this will cause additional allocations and more GC pressure.
String ToLower, ToUpper
Summary. We implemented a case-insensitive string Dictionary. You do not need a custom IEqualityComparer, although if your requirements are slightly unusual this can help.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 10, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen