Home
C#
Dictionary Case-Insensitive
This page was last reviewed on 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
Shows a 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.Shows a dictionary
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 10, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.