StringDictionary keys are not case sensitive
01 Mar 2010I stumbled upon this little quirk in the .NET framework today and thought I'd post about it as it had me stumped for a while.
I was passing a StringDictionary to a method and then looping through the keys and using them to replace key placeholders in a piece of text with the keys associated value.
However the result was that nothing appeared to have happened to the text that was supposed to be amended. After checking my keys matched the placeholders in the text I fired up VS in debug mode and started checking the values at runtime. It was then that I was surprised to find that the keys in the StringDictionary's key collection were all lowercase even though they had been added in Pascal case.
checking MSDN revealed that this is the standard behaviour in StringDictionary object.
The key is handled in a case-insensitive manner; it is translated to lowercase before it is used with the string dictionary
I am not entirely sure why the StringDictionary has this behaviour and it doesn't seem that intuitive. Regardless, a simple workaround is to use Dictionary