Hi there,

In a project I'm currently developing, we used the StringDictionary (In the System.Collections.Specialized namespace) class frequently for stored key value pairs. I noticed that all my keys were lower-cased.

So when checking out the Add method with Reflector, I found out that the key is lower-cased.

public virtual void Add(string key, string value)
{
    if (key == null)
    {
        throw new ArgumentNullException("key");
    }
    this.contents.Add(key.ToLower(CultureInfo.InvariantCulture), value);
}

As described on MSDN: The key is handled in a case-insensitive manner; it is translated to lowercase before it is used with the string dictionary.

So if you want to use a string as a key and want that key string representation exactly to the same as you stored it, that use the following:

// Use the Dictionary<string, string> instead of the StringDictionary, since StringDictionary stores it's key as case-insensitive
Dictionary<string, string> items = new Dictionary<string, string>();

Nothing fancy, all standard .NET functionality, but this post is written as a reminder for myself and for you as a visitor of my blog.

Hope this is usefull!

gr,

Robbert

kick it on DotNetKicks.com

 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList