Search code examples
c#.netcollectionsdictionarystringdictionary

In C# .NET, is there a reason for no copy constructor for StringDictionary?


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?


Solution

  • 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.