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 over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

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