In this article, we will discuss the difference between the Dictionary vs Hashtable in C# with examples. We will start with a brief introduction to data structures, coding implementations and then proceed to the comparison to each of them. Their usage and useful practical coding examples will help us to decide when and what should be used in our code.
The main difference between Dictionary and Hashtable is that dictionary is a generic type collection defined under System.Collection.Generics namespace which also stores data in the form of key/value pairs. Whereas, There is no requirement that the key and value in a hash table must be of the same data type; data of different data types can be stored as keys and values. Additionally, the type of the key and its value does not need to be specified. And the HashTable is the non-generic type of collection that is also used to store data in key/value pair and is defined in the System.Collections namespace.
Dictionary in C#:
A dictionary is a generic collection that can be used to store key/value pairs in C#. Dictionary operates quite similarly to non-generic hashtables. The distinction is that we must specify the type for both the keys and the values when building the generic dictionary object and the advantage of a Dictionary is, it is a generic type. The dictionary's size automatically increases when more entries are added to the generic Dictionary<TKey, TValue> collection due to its dynamic nature. The dictionary class is defined under the namespace System.Collections.Generic.
The Dictionary() constructor is used to create an instance of the Dictionary class that is empty and has the default initial capacity. Let's see how to use the Dictionary() in c# to create an instance of the Dictionary class.
The System.Collections.Generic namespace must be imported before the Dictionary class can be used in your program.
an instance of the Dictionary<TKey, TValue> class using the Dictionary() constructor as follows:
This is the way to create a dictionary constructor. The class is defined in the code to create a dictionary with string types for both the keys and the values.
Given below is a sample example to understand the coding structure of dictionary implementations.
Hashtable in C# :
A hashtable is a group of key/value pairs that are sorted according to the key’s hash code. Or, to put it another way, a collection that stores data in a hash table is created using a hashtable. It is the non-generic type of collection that is defined in System.Collections namespace. As long as they are used as keys in the hashtable, key objects in the hashtables must be immutable.
The HashTable class offered by the .Net Framework includes all the features needed to construct a hashtable without the need for additional coding. A collection of all-purpose dictionaries is called a hashtable. The DictionaryEntry objects in the collection each have two properties: a key object and a value object. Key/Value pairs are what these are called. A hash code is produced automatically when items are added to a hash table. The key object serves as the sole method of accessing the values in the table. The collection's components should be regarded as being randomly organized because they are arranged according to the concealed hash code.
Methods of Hashtable:
Add(): Adds an item with a key and value into Hashtable.
Remove(): Removes the item with the specified key from the Hashtable.
Clear(): Removes all the items from the Hashtable.
Contains(): Checks whether the hashtable contains a specific key.
ContainsKey(): Checks whether the hashtable contains a specific key.
ContainsValue(): Checks whether the hashtable contains a specific key.
Below is the syntax of Hashtable
The System.Collections namespace is where the Hashtable class resides. Therefore, using the "using" keyword, we must first include the System.Collections namespace in our program. The steps to do this are as follows.
The next step is, we have to create an instance of the Hashtable class using the Hashtable constructor as follows.
Use the Add() function of the Hashtable class if you wish to add elements, such as a key/value pair, to the hashtable.
To add an element with the supplied key and value to the Hashtable, use the Add(object key, object? value) method. The element to be added's key is specified by the parameter key, and its value is specified by the parameter value in this case. The value can be empty.
Difference between Dictionary and Hashtable in C#:
The dictionary is included in the System.Collections.generic namespace.
Hashtable is included in the System.Collections namespace.
A dictionary is a generic collection. So it can store key-value pairs of specific data types.
Hashtable is a loosely typed (non-generic) collection, this means it stores key-value pairs of any data type.
A Dictionary is not thread-safe.
Hashtable is thread-safe.
The Dictionary collection is faster than Hashtable since there is no boxing and unboxing.
In Hashtable, the data retrieval is slower than in Dictionary due to boxing and unboxing.
A dictionary maintains the order of stored values.
Hashtable never maintains the order of stored values.
The dictionary will throw an exception if you try to find a key that doesn’t exist.
In the Dictionary, there is no need for boxing/unboxing.
Hashtable returns null when trying to find a key that does not exist. Values in Hashtable need boxing/unboxing.
A dictionary can support multiple readers concurrently, as long as the collection doesn’t modify.
Hashtable is thread-safe and can be used by multiple reader threads and a single writer thread.
A dictionary is a generic collection that can be used to store key/value pairs in C#. Dictionary operates quite similarly to non-generic hashtables. The distinction is that we must specify the type for both the keys and the values when building the generic dictionary object and the advantage of a Dictionary is, it is a generic type. Whereas a hashtable is a group of key/value pairs that are sorted according to the key’s hash code. Or, to put it another way, a collection that stores data in a hash table is created using a hashtable.
hope you enjoyed reading this article and found it useful. Please share your thoughts and recommendations in the comment section below.