StringDictionary vs Dictionary<string, string>

Generics.Net 3.5CollectionsDictionaryStringdictionary

Generics Problem Overview


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?

Generics Solutions


Solution 1 - Generics

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.

Solution 2 - Generics

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"];

Solution 3 - Generics

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.

Solution 4 - Generics

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/

Solution 5 - Generics

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

Solution 6 - Generics

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

Solution 7 - Generics

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScott IveyView Question on Stackoverflow
Solution 1 - GenericsmmxView Answer on Stackoverflow
Solution 2 - GenericsjoshcomleyView Answer on Stackoverflow
Solution 3 - GenericsJeff RoeView Answer on Stackoverflow
Solution 4 - GenericsReed CopseyView Answer on Stackoverflow
Solution 5 - GenericsArekBeeView Answer on Stackoverflow
Solution 6 - GenericsDanielView Answer on Stackoverflow
Solution 7 - GenericsMatt Lyons-WoodView Answer on Stackoverflow