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.
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.
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).