Difference between HashTable and Dictionary in C#


Both HashTable and Dictionary are the type of data structure which are used to store data. Both of these data structures hold the stored data in key value pair.

On the basis of difference between key features of these we can distinguish between HashTable and Dictionary as follows −

Sr. No.KeyHashTableDictionary
1DefinitionHashTable is the non-generic type of collection which is used to store data in key/value pair and is defined in System.Collections name space.On other hand Dictionary is a generic type collection defined under System.Collection.Generics name space which also store data in the form of key/value pairs.
2DataTypeIn HashTable same or different data type data can be stored as key and value there is no restriction that key and value must of same DataType then only could get stored in the HashTable. Also there is no need to specify the type of the key and value.On other hand in case of Dictionary key and value must be of same DataType then only could get stored in dictionary and must specify the type of key and value at the time of creation.
3Data RetrievalIn case of HashTable the data retrieval is slower due to boxing and unBoxing as compared to that in Dictionary.On other hand in case of Dictionary data Retrieval is faster as no boxing and unBoxing is take place in case of Dictionary.
4Null ValuesIn case of HashTable null value handled as on access a key that doesn’t present in the given HashTable then null values returned as result.On other hand, in case of Dictionary it will give error if try to access a key that doesn’t present in the given Dictionary.
5Data OrderHashTable does not maintain any order of the inserted key value data.On other hand Dictionary maintains the insertion order of stored value.

Updated on: 09-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements