Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive Dictionary with char key in C#

If I have a Dictionary<char,...> is it possible to make method ContainsKey case-insensitive?

I know, in Dictionary<string,...> is used StringComparer.InvariantCultureIgnoreCase, but what to do when key in dictionary is char-type?

like image 673
Lenka Avatar asked Nov 29 '22 14:11

Lenka


1 Answers

The Dictionary class has a constructor that takes any IEqualityComparer. What you need to do is implement a simple case-insensitive IEqualityComparer<char> and pass it to the constructor, and it will be used when evaluating the key.

This is a similar question for implementing IComparer<char> without case sensitivity. IEqualityComparer will be practically identical:

  • What is the correct way to compare char ignoring case?
like image 90
Avner Shahar-Kashtan Avatar answered Dec 17 '22 03:12

Avner Shahar-Kashtan