Difference Between Dictionary and HashTable

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

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال