Dictionary Key Case Sensitivity in C#

by Bradley 8. May 2009 16:26

By default you must make sure that a key used to look up a value in a dictionary matches the case of key used to store the value in a dictionary. Simply put, list["A"] would not return the same value as list["a"]. It would be safe to assume that the default behavior was selected for efficiency. After all would you want your code slowed down by doing list["a".ToUpper()], if you knew that the key used to look up a dictionary value would never have a different case than what was used to store it. With that in mind, if case were an issue, most of us would change our code to perform the ToUpper just described. Although this approach is functionally sound, your code could become unnecessarily cluttered. An equally functional approach that would keep your code uncluttered would be to use one of the alternate constructors for your dictionary, see example below.

using System.Collections.Generic;

Dictionary<string, string> list = 
    new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
list.Add("A", "Value");

if (list["A"] != null)
{
    Console.WriteLine("A can be A");
}

if (list["a"] != null)
{
    Console.WriteLine("a can also be A");
}

Tags: , ,

.NET | C#

Comments

7/7/2010 9:51:59 PM #

Immigrants from the European Union coming to Sweden can check the Union's information "Dialogue with citizens" to settle or study in another EU-country

visia info

7/9/2010 7:36:50 AM #

I really think that you've done a good job with this website it looks really good and you have a ton of great information as well, I know I found what I was searching for anyway. Just thought I would take the time to comment, again keep up the good work

May Levy

8/11/2010 6:49:02 AM #

I don’t agree with most people here; I found this post I couldn't stop until I was done, although it wasn't just what I had been searching for, was still a great read though. I will instantaneously take your feed to maintain informed of future updates.

making money on the web

8/12/2010 2:07:06 AM #

You shouldn't have. If gratitude is the soul of nobles, you have made me a King. It's exactly what I wanted.  

Yasmin Gouthier

8/14/2010 1:29:01 AM #

I don’t agree with most people here; I found this blog post I couldn't stop until , while it wasn't just what I had been looking for, was indeed a fantastic read though. I will instantly grab your feed to maintain in touch of any updates.

making money on the web

8/18/2010 8:47:16 AM #

Totally new to me; I wasn't aware of the many ripples and depth to this case until I searched here through Bing! Fantastic job.

Virility Ex

8/18/2010 3:06:53 PM #

did you know koi carp gang rape goldfish when it gets dark :O

Philip

8/23/2010 2:34:49 PM #

how can I advertise on this site?

Patsy Baker

8/24/2010 12:56:01 PM #

Really first-class  objects.

romford dentist

8/27/2010 12:24:58 AM #

She is just fantastic! Everything she touches turns to gold, so in spite of some rumors or fabrications about her, she remains on the top. So go girl, we love you

how to make a woman squirt

8/30/2010 8:58:54 AM #

I am just getting my feet wet in community management/marketing media and starting to learn how to do it well - resources like this article are incredibly helpful. As our website is based in the US, is kind of new to us The reference above is something that I worry too well, how to show your own authentic enthusiasm and share to the opportunity.

bowtrol

8/30/2010 9:41:07 PM #

Easily, the post is actually the greatest on this noteworthy topic. I agree with your conclusions and will eagerly look forward to your approaching updates. Just saying thanks will not just be sufficient, for the phenomenal lucidity in your writing. I will immediately grab your rss feed to stay privy of any updates. Pleasant work and much success in your  business endeavors!

Jewell

9/1/2010 1:38:56 AM #

In point of fact, I’m just getting my feet wet in marketing media and trying to find out how to do it well - resources like this article are a great resource. As our website is based in the US, it’s all a bit new to us. The reference above is something that I worry too well, how to show your own authentic enthusiasm and share to the solution.

virility ex

9/2/2010 3:32:01 AM #

Thanks for making the truthful effort to explain this. I feel very strong about it and would like to nkwo more. If it's OK, as you learn more in depth knowledge, would you mind posting more articles similar to this one with more tips?

niche blue print reviews

Add comment




biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Original Theme by Mads Kristensen | Modified by Crafty Coders