Difference Between Dictionary and HashTable

Kailash Chandra Behera | Saturday, May 14, 2016

Introduction

This article describes measure differences that effects our progremming life cycle between Dictionary and HashTable. Both Dictionary and HashTable are collection data structure that store data in key value pairs. Below details contains the measure differences.



Differences

Type

Dictionary is generic type and strongly typed data structure, hence you must specify the data type for the key and value.

HashTable is not generic type and weakly typed data structure, hence you need not define the data type for key and value.

Declaration

 Dictionary<int, string> dictionary = new Dictionary<int, string >();  

 Hashtable numbers = new Hashtable();  



Performance

As dictionary is strongly typed data type boxing and unboxing does not happence in dictionary, hence it is faster than HashTable.

As hastable is weakly typed data type boxing and unboxing happence in hastable, which causes memory consumption and performance penality.

Thread safe

Dictionary is not thread safe, custome synchronization need to impliment if thread safe is required.

HashTable is thread safe, it supports multiple reader thread and a single writer thread. Hence hastable allows one writer toghter with multiple reader thread.




Maintenance

When we add the multiple entries in Dictionary, the order in which the entries are added is maintained. When we retrieve the items from Dictionary we will get the records in the same order we have inserted them

Whereas we add same records in Hashtable the order is not maintained. From the following example you can see the difference in the order of retrieval.

Summary

Hope you have got the measure differences exist between Dictionary and HashTable.

Thanks