Mar 10, 2015

Hashtable vs Dictionary



1. Hashtable is threadsafe and while Dictionary is not.
2. Dictionary is types means that the values need not to boxing while Hashtable
    values  need to be boxed or unboxed because it stored the  values and keys as
    objects.        
3. When you try to get the value of key which does not exists in the collection, the
    dictionary throws an exception of 'KeyNotFoundException' while hashtable returns
    null value.
4. When using large collection of key value pairs hashtable would be considered more
    efficient than dictionary.
5. When we retrieve the record in collection the hashtable does not maintain the order
    of entries while dictionary maintains the order of entries by which entries were added.


6. Dictionary relies on chaining whereas Hashtable relies on rehashing.


Dictionary:


  • It returns error if we try to find a key which does not exist.
  • It is faster than a Hashtable because there is no boxing and unboxing.
  • Only public static members are thread safe.
  • Dictionary is a generic type which means we can use it with any data type.

Hashtable:


  • It returns null if we try to find a key which does not exist.
  • It is slower than dictionary because it requires boxing and unboxing.
  • All the members in a Hashtable are thread safe,
  • Hashtable is not a generic type,

No comments:

Post a Comment