HashTable v. Dictionary

When storing sets of key-value pairs, do not use the HashTable class. Do use the Dictionary class. This assumes you are storing sets of un-ordered key-value pairs where the key is a value Type and have already decided there is no other better choice, e.g. List, SortedList, HashSet, ArrayList. A list of Collection Types can be found here: Commonly Used Collection Types.

The Dictionary class was introduced in .NET 2.0 and effectively renders the HashTable class obsolete.

From the MSDN article Hashtable and Dictionary Collection Types:

The Dictionary and ConcurrentDictionary classes have the same functionality as the Hashtable class. A Dictionary of a specific type (other than Object) provides better performance than a Hashtable for value types. This is because the elements of Hashtable are of type Object; therefore, boxing and unboxing typically occur when you store or retrieve a value type.

Advertisements