Difference between Hashtable and Dictionary

C# Dictionary and Hashtable

Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type. The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable. The Dictionary class is a strongly types < T Key, T Value > and you must specify the data types for both the key and value.

C# Declaration

VB.Net Declaration

While perform operations Dictionary is faster because there is no boxing/unboxing (valuetypes don't need boxing) while in Hashtable boxing/unboxing (valuetypes need boxing) will happened and which may have memory consumption as well as performance penalties.

Another important difference is that Hashtable Hashtable is thread safe for supports multiple reader threads and a single writer thread. That is the Hashtable allows ONE writer together with multiple readers without locking. In the case of Dictionary there is no thread safety, if you need thread safety you must implement your own synchronization.

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.

How to C# Dictionary

C# Dictionary and Hashtable

A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys. More about.... C# Dictionary

Iterate over a Dictionary

There are many different ways to iterate over a Dictionary in C#. From the following link you can see in detail .... How to Iterate over a Dictionary

Dictionary Versus List

C# Dictionary and List

Both lists and dictionaries are used to store collections of data. The Dictionary is based on a hash table, that means it uses a hash lookup, which is a rather efficient algorithm to look up things, on the other hand, a list you have to go element by element until it finds the result from beginning to the result each time. More about.... Dictionary vs List
 (C) 2022    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About