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?
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.
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.
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.
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.
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"];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With