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: c# Dictionary: making the Key case-insensitive through declarations

| |
  • 5
    What is wrong with using StringComparer.InvariantCultureIgnoreCase? It does what it says... – leppie Dec 21 '12 at 10:49
  • 5
    I'd never heard of it, hence the question! – Mr. Boy Dec 21 '12 at 11:12
  • 2
    Possible duplicate of Case insensitive access for generic dictionary – Michael Freidgeim May 12 '16 at 7:33
  • That question is related, but not quite a duplicate of this one. That one gets into how to deal with an existing dictionary. I'm starting with new code, so the answers here are better suited. – goodeye Feb 27 '19 at 0:00


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>(

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.

| |
  • 1
    Perfect, I had somehow missed STL-like way of passing the comparator into the ctor. – Mr. Boy Dec 21 '12 at 11:13
  • 7
    It never ceases to amaze me how I always find the answer to any coding question on here! Maybe I'll name my next cat after you, Konrad! :-) – Jamie Jun 3 '14 at 21:36
  • 10
    Possible, the StringComparison.OrdinalIgnoreCase comparer faster than culture-based: stackoverflow.com/questions/492799/… – Manushin Igor Mar 30 '15 at 12:03
  • How do I do it if I am not creating a dictionary but trying to use ContainsKey() for a case insensitive key match on an existing dictionary that I get as a part of an object? – Shridhar R Kulkarni Jun 22 '18 at 9:41
  • 1
    @ShridharRKulkarni You fundamentally can't (efficiently). The comparison logic is a core part of the internal dictionary data structure. To allow this, the container would have to maintain multiple indices for its data, and dictionaries don't do this. – Konrad Rudolph Jun 22 '18 at 10:53
var myDic = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
myDic.Add("HeLlo", "hi");

if (myDic.ContainsKey("hello"))
| |
  • 1
    Code snippet says it all. Very helpful. Not sure why this post has so less upvotes as compared to the accepted answer just for being late by 3 minutes. – RBT Apr 6 '18 at 11:36

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

| |
  • 3
    This works great. A word of caution though, if you change Any to SingleOrDefault you will not get null back if it doesn't exist, instead you will get a keyvaluepair with both key and value set to null! – NibblyPig Mar 2 '17 at 14:10
  • 1
    Contains Seems like a very specific use-case to something you were working on at the time. As a more generic helpful answer, I think Equals is better. And on that same note, instead of duplicating a string with ToLower(), It would be even better to use StringComparison.xxxCase. – Suamere Feb 6 '18 at 17:37
  • 1
    this was very useful and getting rid of the ToLower is definitely an improvement - my use case was: return AppliedBuffs.Any(b => b.Key.Equals(Name, StringComparison.OrdinalIgnoreCase)); a perfect case-insensitive Contains on a case sensitive dictionary. – David Burford Oct 2 '18 at 10:53
  • I am so tempted downvote this. Word of caution, if you are the owner of the dictionary this is most certainly not the way to do it. Only use this approach if you dont know how the dictionary was instantiated. Even then, for exact match of string (not just part match), use dict.Keys.Contains("bla", appropriate comparer) LINQ overload for ease of use. – nawfal Jul 23 '19 at 11:26

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:


Then get a value from the dictionary with:

| |

Your Answer

