C# Dictionary and case insensitive ContainsKey checking

04 Feb

The default constructor of C# Dictionary class constructs a Dictionary object, in which the keys are case sensitive. So when you insert data pairs <Key, Value> and <key, Value>, they are regarded as two different items. To make the key (in many case, strings) case insensitive, so that when you query both Key and key, the same Value is offered, simply use a parameter in the constructor:

Dictionary<string, long> SomeDict=new Dictionary<string, long>(StringComparer.InvariantCultureIgnoreCase);

This works.


Posted by on February 4, 2009 in Dotnet/C#


16 responses to “C# Dictionary and case insensitive ContainsKey checking

  1. remedytomm

    April 16, 2009 at 2:19 pm

    It strikes me that since dictionaries use hash based lookups, this is a very inefficient procedure..

    It would be much preferred to plan your application a little better and convert your keys to upper or lower case on add, and lookup using the same case.

  2. xinyustudio

    April 16, 2009 at 2:28 pm

    String.ToUpper and String.ToLower are also quite expensive. Have you ever tested the difference of using both apporaches? I am very interested to know the results.

  3. Dustin

    August 6, 2010 at 1:49 am

    Thanks for this info, VERY VERY helpful!

  4. dave

    October 5, 2011 at 5:28 pm

    if i were implementing a case insensitive dictionary i would just convert all keys to upper case on the way in, that way you can still use hashing and it wouldn’t be inefficient at all.

  5. xinyustudio

    October 5, 2011 at 7:02 pm

    If you have ever done some benchmarking, can you offer quantitative comparison as to efficient vs. inefficient? I suspect once you have done so, you might have a better idea on this issue. If you just perform only tens of such query, you can of course neglect it. What we are talking about here is absolutely for geek users who tries to find the best solution with the most economic way.

  6. dave

    October 5, 2011 at 7:06 pm

    i don’t need to do any benchmarking because i know what the performance would be – plus one cheap operation (string.toupper) on insertion, plus one cheap operation (again string.toupper) on retrieval.

  7. xinyustudio

    October 5, 2011 at 7:09 pm

    Whatever, as long as you feel comfortable. As mentioned earlier, ToUpper() is straightforward, but not the most elegant approach. Happy coding!

  8. dave

    October 5, 2011 at 7:44 pm

    I was just speculating as to that being the actual method of implementation of the .net case insensitive dictionary as it would be quite reasonable. Turns out we’ll probably never know as the hashes are actually calculated using native code:

    internal static extern unsafe int nativeGetGlobalizedHashCode(void* pSortingFile, string pString, int dwFlags, int win32LCID);

  9. Woric

    April 3, 2012 at 5:35 pm

    I just did some quick testing comparing the different ways of using of adding and retrieving 250,000 dictionary entries on Dictionary, and here are the results on my machine.

    For comparison, using a CaseSensitive Dictionary with the correct case key took 58ms
    Using a Dictionary with StringComparer.OrdinalIgnoreCase took 101ms
    Using a CaseSensitive Dictionary and using ToUpper() for each key add/retrieve took 164ms
    Using a Dictionary with StringComparer.InvariantCultureIgnoreCase took 199ms

    Hope that puts it to rest.

  10. xinyustudio

    April 4, 2012 at 9:29 am


  11. Ben

    September 24, 2012 at 10:59 pm

    As always you should do your own profiling. My numbers are quite different from yours, Woric and result in .ToUpper() being faster by about 1/3rd.

  12. xinyustudio

    September 25, 2012 at 1:04 pm

    We are talking about general performance, not case specific one. It is at your absolute discretion to use whatever ways you are comfortable with.

    Ben, if your results are significantly different from Woric’s, can you let us know what input have you used and what is your result? Your results might be valuable to us all. Cheers, 🙂

  13. Andy

    October 18, 2012 at 3:31 am

    I wouldn’t think this would be any less performant; GetHashCode is still used and the StringComparer has a method of getting a hash code in a case insensitive way. This was just the easy solution I was looking for!

  14. xinyustudio

    October 18, 2012 at 11:23 am

    Andy, Did you see Woric’s comment above? How do think of his data?

  15. abdus samadh

    December 24, 2012 at 8:25 pm

    thanks for the useful information dude……….


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: