Dictionary
and Hashtable
differences:
and
difference Hashtable
Dictionary
Dictionary |
Hashtable |
Support Paradigm |
Not supported |
Need to do thread synchronization yourself |
Thread-safe types can be obtained by calling the Synchronized () method |
Enum type is KeyValuePair |
Enumeration type isDictionaryEntry |
. Net 2.0 starts with |
. Net 1.0 starts with |
Under the System.Collections.Generic namespace |
Under the system.collections namespace |
Throws an exception when getting the same nonexistent key |
Returns NULL when getting the same nonexistent key |
Value types do not need to be boxed/unboxing and may be faster |
Value type values types need to be boxed/unboxing boxing/unboxing |
Same point :
Internal is hashtable implementation, through key access to data faster
Need to be immutable, the only key
Key all need to have their ownGetHashCode()
方法
other similar. Net collections (for alternative Dictionary和Hashtable的
alternatives)
ConcurrentDictionary
-For multi-concurrency, thread-safe dictionaries
Under the Hybriddictionary-system.collections.specialized namespace, performance is optimized (when the collection is small, use listdictionary to implement IDictionary, and then when the collection becomes larger, Switch to Hashtable. )
Under the Ordereddictionary-system.collections.specialized namespace, the value can be accessed by a numeric index because the element is already specified when it is added. public void Insert (int index, object key, object value);
SortedDictionary-Automatically sorted when an element is inserted
Under the Stringdictionary-system.collections.specialized namespace, the keys and values are strongly typed as strings rather than objects, and the corresponding optimizations are made.
Some similarities and differences of dictionary and Hashtable