Hashtable vs. Dictionary in C#


Hashtable

A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

The members in a Hashtable are thread safe. It returns null if we try to find a key that does not exist. Hashtable is not a generic type.

The Hashtable collection is slower than dictionary because it requires boxing and unboxing.

To declare a Hashtable −

Hashtable ht = new Hashtable();

Dictionary

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace. 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.

To declare a Dictionary −

IDictionary<int, string> d = new Dictionary<int, string>();

Updated on: 20-Jun-2020

854 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements