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

ProgrammingServer Side ProgrammingCsharp

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")
raja
Published on 11-Sep-2018 08:56:35
Advertisements