Why is a Dictionary preferred over a Hashtable in C#?


Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.

Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collection.Generics namespace.

Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.

Let’s say we need to find a key from Hashtable collections. With that, we are also finding a key from Dictionary collection as well. In that case, Dictionary would be faster for the same statement −

For HashTable

hashtable.ContainsKey("12345");

For Dictionary

dictionary.ContainsKey("12345")

Updated on: 22-Jun-2020

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements