Data Haunting

Easy Solutions Destination

By

Difference between Hashtable and Dictionary in c#

Hashtable and Dictionary both are collection of Data Structure to hold data as key and value pair but Hashtable and Dictionary both are difference in terms of use and structure. In this article we will see Difference between Hashtable and Dictionary with example and when we have to use Hashtable and Dictionary in c#.

Difference between Dictionary and Hashtable

Dictionary Hashtable
Dictionary is Genereic type which means we can use any data type with Dictionary. Hashtable is not generic type.
Dictionary is faster than Hashtable as it does not required boxing and unboxing. Hashtable is slower than Dictionary as it required boxing and unboxing.
Only public static members are thread safe. All members are thread safe in Hashtable.
It throws error if we try to find a key which does not exist. It returns null if we try to find a key which does not exist.
Dictionary is not efficient for large collections of Key and Value pair. Hashtable is efficient for large collections of Key and Value pair.
Dictionary maintains the order of entries while we retrieve the record from Dictionary by which entries were added. Hashtable does not maintain the order(most probably in reverse order) of entries while we retrieve the record from Hashtable by which entries were added .
Dictionary relies on chaining. Hashtable relies on rehashing.

Hashtable Example

Hashtable optimize lookups. It computes hash of each key you added than use this hash code to look up the element very quickly. Hash table is not generic type, hash table is weakly type data structures.

From the above example of Hashtable’s Output we can see that Hashtable is not maintaining the order of entries as it is entered while retrieving from the record.

Dictionary Example

Dictionary is useful where fast lookups are critical. Dictionary type provide fast lookups with keys to get value. Dictionary is faster than Hashtable and use generic type with strongly type data structures.

From the above example of Dictionary’s Output we can see that Dictionary is maintaining the order of entries as it is entered while retrieving from the record.

One of the another important different which we have specified earlier in above table is that Hashtable is thread safe it means it supports multiple reader threads and a single write thread. That is Hashbtable allow one writer together with multiple reader without locking. In case of Dictionary there is no thread safety, if we need thread safety we have to implement our own synchronization. In Dictionary only public static members are thread safe.

What is your opinion ?

Here we have compare Hashtable and Dictionary so from that we can have idea that when we have to use Hashtable and Dictionary. I hope it will be very useful to understand Hashtable vs Dictionary. If you have any query you can comment or you can mail me.

One Response to Difference between Hashtable and Dictionary in c#

Leave a Reply