StringDictionary vs Dictionary<string, string>
Asked Answered
C

7

83

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?

Chuddar answered 9/3, 2009 at 19:44 Comment(0)
C
97

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.

Crock answered 9/3, 2009 at 19:49 Comment(3)
You should benchmark to see which performs better for your special situation. I don't expect a noticeable performance difference. I recommend using Dictionary<string,string> for all new code (unless you need to target 1.1).Crock
@thecoop no hard data to present from my side now, but once when I did test for speed StringDictionary performed worse (though all that wont matter mostly). To be honest in fact, earlier I had the notion that StringDictionary was some sorta specialized collection meant for speeding things when string was the key, and to my great surprise when it performed worse in my testing, I googled it up only to learn it is nongenericSanctuary
@Sanctuary I think it's slower because StringDictionary is case-insensitive by default. If you create a Dictionary with case-insensitive key comparer then I believe performance will be the sameFomentation
D
44

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"];
Detroit answered 12/1, 2011 at 14:33 Comment(6)
The "throws an exception" feature ofDictionary<> means i can never use []. Use StringDictionary has cleaned up much of my code.Mattingly
@James I'm a little late but you should probably use TryGetValue.Counterbalance
@ŞafakGür -- I do use TryGetValue() -- because I can never use [ ] because it throws an exception. (StringDictionary only solves this for one type of Dictionaries)Mattingly
@James, I believe throwing an exception for non-existent keys is a better API choice since null is allowed as a dictionary value. This way you can distinguish between a key that does not exist from one whose value is null.Counterbalance
@ŞafakGür this is true -- if your need is for a dictionary which a) should always have all values, b) can contain nulls and c) those null means something diffeent than "no value".. However,in a dictionary "no value" is usually represented by the key not being there. However, the real reason Dictionary<> throw an exception, is because the value could be a value type (and therefore, can't be represented with a null).Mattingly
The reason a generic dictionary throws when the key doesn't exist is because if it returned default(TValue) instead, it would be impossible to distinguish between the dictionary having an item with a Value of default(TValue) or containing no KeyValuePair matching the Key. This is why using TryGetValue makes sense most of the time. Do note that it is possible to get back the behavior that doesn't throw if the Key doesn't exist by using ((IDictionary)dic)["Hey"];, but you should only do this for reference types (if ever).Leucine
L
41

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/

Lat answered 9/3, 2009 at 19:50 Comment(1)
StringDictionary can be used in Properites.Settings while Dictionary<string,string> cannot. So obsolete or not, there is still some use for StringDictionary.Marmoset
C
41

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.

Clang answered 22/7, 2011 at 17:28 Comment(1)
If for some reason one want the same behaviour with Dictionary<string, string> this can be used: Dictionary<string, string> ht = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); ht.Add("Bob", "ff"); ht.Add("bob", "ff"); // throws exceptionReflex
W
4

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
Whitesell answered 10/11, 2011 at 14:44 Comment(0)
P
1

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

Permenter answered 15/2, 2011 at 23:15 Comment(1)
we could use some numbers hereRambouillet
K
1

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.

Kamal answered 12/11, 2012 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.