Case Insensitive DictionaryCreate a case-insensitive Dictionary with StringComparer.OrdinalIgnoreCase.
This page was last reviewed on Nov 25, 2022.
Case, Dictionary. A case-insensitive Dictionary is ideal in some programs. It helps with comparing file names in Windows, which ignore case.
Notes, upper and lower. Sometimes user names are case-insensitive—upper and lower letters are treated as equal. We implement a case-insensitive string Dictionary.
An example. The Dictionary class has several overloaded constructors. Here we use one that accepts a parameter of type IEqualityComparer(string).
IEqualityComparer This object implements an interface and is used by Dictionary to acquire hash codes and compare keys.
New We use a Dictionary constructor that accepts an IEqualityComparer. We specify StringComparer.OrdinalIgnoreCase as the comparer.
Methods StringComparer.OrdinalIgnoreCase implements (at minimum) 2 methods, Equals and GetHashCode.
StringComparison, StringComparer
Info These 2 methods are used internally by the Dictionary to compute hash keys and compare buckets.
using System; using System.Collections.Generic; class Program { static void Main() { // Create case insensitive string Dictionary. var caseInsensitiveDictionary = new Dictionary<string, int>( StringComparer.OrdinalIgnoreCase); caseInsensitiveDictionary.Add("Cat", 2); caseInsensitiveDictionary.Add("Python", 4); caseInsensitiveDictionary.Add("DOG", 6); // Write value of "cat". Console.WriteLine(caseInsensitiveDictionary["CAT"]); // 2 // Write value of "PYTHON". Console.WriteLine(caseInsensitiveDictionary["PYTHON"]); // 4 // See if "dog" exists. if (caseInsensitiveDictionary.ContainsKey("dog")) { Console.WriteLine("Contains dog."); // <-- Displayed } // Enumerate all KeyValuePairs. foreach (var pair in caseInsensitiveDictionary) { Console.WriteLine(pair.ToString()); } } }
2 4 Contains dog. [Cat, 2] [Python, 4] [DOG, 6]
Notes, usage. 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.
Caution When you add the strings "Cat" and "CAT", you will get an exception. The Dictionary considers the 2 strings to be equivalent.
Note, ContainsKey. ContainsKey (along with TryGetValue) will accept keys of either case. So "PYTHON" will return the same value as "Python".
A discussion. Another approach you can use to normalize string data in your Dictionary is always calling ToLower on the keys and before looking up keys.
File names In Windows, file names are case-insensitive. But when users copy files, they expect them to retain their original case.
Thus This Dictionary is ideal here. It will mimic the behavior of Windows NTFS filesystems.
A summary. We implemented a case-insensitive string Dictionary. You do not need to write your own IEqualityComparer, although if your requirements are slightly unusual you can do so.
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.
No updates found for this page.
© 2007-2023 Sam Allen.