Dictionary is a generic type and returns an error if you try to find a key which is not there. The Dictionary collection is faster than Hashtable because there is no boxing and unboxing.
A dictionary is a data structure that maps keys to values. A hash table is a data structure that maps keys to values by taking the hash value of the key (by applying some hash function to it) and mapping that to a bucket where one or more values are stored.
Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type. The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable.
System.Collections.Generic.Dictionary<TKey, TValue>
and System.Collections.Hashtable
classes both maintain a hash table data structure internally. None of them guarantee preserving the order of items.
Leaving boxing/unboxing issues aside, most of the time, they should have very similar performance.
The primary structural difference between them is that Dictionary
relies on chaining (maintaining a list of items for each hash table bucket) to resolve collisions whereas Hashtable
uses rehashing for collision resolution (when a collision occurs, tries another hash function to map the key to a bucket).
There is little benefit to use Hashtable
class if you are targeting for .NET Framework 2.0+. It's effectively rendered obsolete by Dictionary<TKey, TValue>
.
I guess it doesn't mean anything to you now. But just for reference for people stopping by
Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable
Memory allocation:
Time used for inserting:
Time for searching an item:
Dictionary:
Hashtable:
Another important difference is that the Hashtable type supports lock-free multiple readers and a single writer at the same time, while Dictionary does not.
MSDN Article: "The
Dictionary<TKey, TValue>
class has the same functionality as theHashtable
class. ADictionary<TKey, TValue>
of a specific type (other thanObject
) has better performance than aHashtable
for value types because the elements ofHashtable
are of typeObject
and, therefore, boxing and unboxing typically occur if storing or retrieving a value type".
Link: http://msdn.microsoft.com/en-us/library/4yh14awz(v=vs.90).aspx
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