StringDictionary vs Dictionary<string, string>

Does anyone have any idea what the practical differences are between the System.Collections.Specialized.StringDictionary object and System.Collections.Generic.Dictionary?

I've used them both in the past without much thought as to which would perform better, work better with Linq, or provide any other benefits.

Any thoughts or suggestions as to why I should use one over the other?

Answers


Dictionary<string, string> is a more modern approach. It implements IEnumerable<T> and it's more suited for LINQy stuff.

StringDictionary is the old school way. It was there before generics days. I would use it only when interfacing with legacy code.


Another point.

This returns null:

StringDictionary dic = new StringDictionary();
return dic["Hey"];

This throws an exception:

Dictionary<string, string> dic = new Dictionary<string, string>();
return dic["Hey"];

I think StringDictionary is pretty much obsolete. It existed in v1.1 of the framework (before generics), so it was a superior version at the time (compared to the non-generic Dictionary), but at this point, I don't believe there are any specific advantages to it over Dictionary.

However, there are disadvantages to StringDictionary. StringDictionary lower-cases your key values automatically, and there are no options for controlling this.

See:

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/59f38f98-6e53-431c-a6df-b2502c60e1e9/


As Reed Copsey pointed out, StringDictionary lower-cases your key values. For me this was totatlly unexpected, and is a show-stopper.

private void testStringDictionary()
{
    try
    {
        StringDictionary sd = new StringDictionary();
        sd.Add("Bob", "My name is Bob");
        sd.Add("joe", "My name is joe");
        sd.Add("bob", "My name is bob"); // << throws an exception because
                                         //    "bob" is already a key!
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I'm adding this reply to draw more attention to this huge difference, which IMO is more important than the modern vs. old-school difference.


StringDictionary comes from .NET 1.1 and implements IEnumerable

Dictionary<string, string> comes from .NET 2.0 and implements IDictionary<TKey, TValue>,IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

IgnoreCase is only set for Key in StringDictionary

Dictionary<string, string> is good for LINQ

        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("ITEM-1", "VALUE-1");
        var item1 = dictionary["item-1"];       // throws KeyNotFoundException
        var itemEmpty = dictionary["item-9"];   // throws KeyNotFoundException

        StringDictionary stringDictionary = new StringDictionary();
        stringDictionary.Add("ITEM-1", "VALUE-1");
        var item1String = stringDictionary["item-1"];     //return "VALUE-1"
        var itemEmptystring = stringDictionary["item-9"]; //return null

        bool isKey = stringDictionary.ContainsValue("VALUE-1"); //return true
        bool isValue = stringDictionary.ContainsValue("value-1"); //return false

Besides being a more "modern" class, I noticed that Dictionary is more memory efficient than StringDictionary by a large margin.


Another relevant point is that (correct me if I'm wrong here) System.Collections.Generic.Dictionary isn't able to be used in application settings (Properties.Settings) whereas System.Collections.Specialized.StringDictionary is.


Need Your Help

How to convert hours and minutes to minutes with moment.js?

javascript date momentjs

I need to convert hours and minutes in minutes values. With pure JavaScript Date object I do the following:

How do event listeners work?

c++ c events

Do they repeatedly check for the condition and execute if the condition is met. Ex, how the OS knows exactly when a USB device is plugged in, or how MSN knows exactly when you get an email. How doe...