Difference between Dictionary and Hashtable

blitzkriegz picture blitzkriegz · May 18, 2009 · Viewed 176.8k times · Source

Possible Duplicate:
Why Dictionary is preferred over hashtable in C#?

What is the difference between Dictionary and Hashtable. How to decide which one to use?

Answer

Marc Gravell picture Marc Gravell · May 18, 2009

Simply, Dictionary<TKey,TValue> is a generic type, allowing:

  • static typing (and compile-time verification)
  • use without boxing

If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)

A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) use ConcurrentDictionary<TKey, TValue>.