Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 890
Scott Ivey Avatar asked Mar 09 '09 19:03

Scott Ivey


People also ask

Can a dictionary key be a string in C#?

Add elements to a C# Dictionary Both types are generic so it can be any . NET data type. The following Dictionary class is a generic class and can store any data type. This class is defined in the code snippet creates a dictionary where both keys and values are string types.

Can a dictionary hold different data types?

One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.

Can C# dictionary have different data types?

In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements. The capacity of a Dictionary is the number of elements that Dictionary can hold.

How does C# dictionary work?

A dictionary, also called an associative array, is a collection of unique keys and a collection of values, where each key is associated with one value. Retrieving and adding values is very fast. Dictionaries take more memory because for each value there is also a key.


2 Answers

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"]; 
like image 30
joshcomley Avatar answered Sep 24 '22 00:09

joshcomley


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.

like image 78
mmx Avatar answered Sep 24 '22 00:09

mmx