views:

373

answers:

4

I apologize if this is a dumb question, but hear me out:

Dictionary<string, string> genericDict = new Dictionary<string, string>;
genericDict.Add("blah", "bloop");
// Use the copy constructor to create a copy of this dictionary
return new Dictionary<string, string>(genericDict);

In the above code sample, I can create a copy of a generic dictionary.

Now suppose I'm using a System.Collections.Specialized.StringDictionary, because I don't feel like typing the "string" types everywhere. StringDictionary has no copy constructor! In fact, it only has the default constructor.

Sure, I can iterate through the StringDictionary and add each key/value pair manually, but I don't want to :-P

Why no copy constructor? Am I missing something here?

+8  A: 

The StringDictionary type is rather obsolete. I think that Dictionary<String,String> is what you want to use here.

The Dictionary<TKey,TValue> type implements some strongly-typed interfaces (ICollection<KeyValuePair<TKey, TValue>> and IEnumerable<KeyValuePair<TKey, TValue>>) which makes it more useful than the StringDictionary type.

While the StringDictionary type is strongly typed I wouldn't advise its use for the sake of laziness alone.

Andrew Hare
Yes, but isn't that for the generic Dictionary<typeKey, typeValue>, not for the specialized StringDictionary?I was just wondering why that very handy overload wasn't implemented for the specialized StringDictionary.
Pandincus
I think he's saying that the generic type DOES have this sort of constructor, but the old specialized dictionary does not.
Keltex
@Pandincus: Keltex is correct - the specialized dictionary does not that overloaded constructor as it is an older type and most likely the copy constructor was not considered at that time. Regardless, I am highly recommending that you use `Dictionary<String,String>` and abandon `StringDictionary`.
Andrew Hare
I guess laziness only gets you so far. OK, you've convinced me. :-D
Pandincus
+3  A: 

If you really want to use a StringDictionary (perhaps to support a legacy application), you can create an extension method:

public static StringDictionary NewCopy(this StringDictionary olddict)
{
    var newdict = new StringDictionary();
    foreach (string key in olddict.Keys)
    {
        newdict.Add(key, olddict[key]);
    }
    return newdict;
}

Then you can do this:

StringDictionary newdict = olddict.NewCopy();
Keltex
+1  A: 

If you don't want to type the "string" types anywhere but are otherwise happy with Dictionary<string,string>, then create a new class that subclasses Dictionary<string,string> and just don't override any methods. You can even call it StringDictionary, as long as it's in a different namespace.

Tadmas
+2  A: 

I agree with the consensus -- you're better off using Dictionary<string, string>. But if you don't like typing the generic types all the time, you could create your own subclass:

public class StringDict : Dictionary<string, string>
{
  // duplicate all Dictionary's constructors here, calling the base constructor for each. 
}

Et Voila! Whenever you want to use a string Dictionary, you use StringDict instead.

Randolpho
I think that StrigDict offers a more compelling feature set.
quillbreaker
Oh, you! Typo fixeded.
Randolpho