Case-INsensitive Dictionary with string key-type in C#

C#CollectionsGeneric Collections

C# Problem Overview


If I have a Dictionary<String,...> is it possible to make methods like ContainsKey case-insensitive?

This seemed related, but I didn't understand it properly: https://stackoverflow.com/questions/6676245/c-sharp-dictionary-making-the-key-case-insensitive-through-declarations

C# Solutions


Solution 1 - C#

> This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations

It is indeed related. The solution is to tell the dictionary instance not to use the standard string compare method (which is case sensitive) but rather to use a case insensitive one. This is done using the appropriate constructor:

var dict = new Dictionary<string, YourClass>(
        StringComparer.InvariantCultureIgnoreCase);

The constructor expects an IEqualityComparer which tells the dictionary how to compare keys.

StringComparer.InvariantCultureIgnoreCase gives you an IEqualityComparer instance which compares strings in a case-insensitive manner.

Solution 2 - C#

var myDic = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
myDic.Add("HeLlo", "hi");

if (myDic.ContainsKey("hello"))
    Console.WriteLine(myDic["hello"]);

Solution 3 - C#

There are few chances where your deal with dictionary which is pulled from 3rd party or external dll. Using linq

YourDictionary.Any(i => i.KeyName.ToLower().Contains("yourstring")))

Solution 4 - C#

If you have no control in the instance creation, let say your object is desterilized from json etc, you can create a wrapper class that inherits from dictionary class.

public class CaseInSensitiveDictionary<TValue> : Dictionary<string, TValue>
{
    public CaseInSensitiveDictionary() : base(StringComparer.OrdinalIgnoreCase){}
}

Solution 5 - C#

I just ran into the same kind of trouble where I needed a caseINsensitive dictionary in a ASP.NET Core controller.

I wrote an extension method which does the trick. Maybe this can be helpful for others as well...

public static IDictionary<string, TValue> ConvertToCaseInSensitive<TValue>(this IDictionary<string, TValue> dictionary)
{
    var resultDictionary = new Dictionary<string, TValue>(StringComparer.InvariantCultureIgnoreCase);
    foreach (var (key, value) in dictionary)
    {
        resultDictionary.Add(key, value);
    }

    dictionary = resultDictionary;
    return dictionary;
}

To use the extension method:

myDictionary.ConvertToCaseInSensitive();

Then get a value from the dictionary with:

myDictionary.ContainsKey("TheKeyWhichIsNotCaseSensitiveAnymore!");

Solution 6 - C#

I know this is an older question, but I had the same issue where the dictionary is coming from a 3rd party tool that did not implement an ignore case StringComparer in the constructor. Tweaked from the method @Soviut has above, but feel this is a lot cleaner and lets you work with the value immediately.

var lookup = source.FirstOrDefault(x => x.Key.Equals("...", StringComparison.OrdinalIgnoreCase));
if (lookup.Key != null)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr. BoyView Question on Stackoverflow
Solution 1 - C#Konrad RudolphView Answer on Stackoverflow
Solution 2 - C#SteveView Answer on Stackoverflow
Solution 3 - C#KurkulaView Answer on Stackoverflow
Solution 4 - C#A.G.View Answer on Stackoverflow
Solution 5 - C#Ferry JongmansView Answer on Stackoverflow
Solution 6 - C#Nayt GrochowskiView Answer on Stackoverflow