C# Difference between Hashtable and Dictionary with Examples

  By : Suresh Dasari
  Posted On : 20-Jun-2023

Here we will learn what is hashtable in c#, what is a dictionary in c#, and what are the differences between hashtable and dictionary in c# with examples.

 

In c#, the dictionary and hashtable both are useful to store the collection of key/value pairs of same or different data type elements.

Hashtable in C#

In c#, hashtable is useful to store and retrieve the collection of key/value pairs of different data type elements. In the hashtable, the order of elements is organized based on the hash code of the key.

Syntax of Hashtable in C#

Following is the syntax of defining the hashtable in c#.

 

Hashtable tbl = new Hashtable()

If you observe the above hashtable syntax, we create a hashtable variable (tbl) with an instance of the hashtable class.

C# Hashtable Example

Following is the example of defining and adding elements to the hashtable in c#.

 

Hashtable htbl = new Hashtable();
htbl.Add("msg", "Welcome");
htbl.Add("site", "Tutlane");
htbl.Add(1, 20.5);
htbl.Add(2, null);

If you observe the above hashtable example, we are able to add the different datatype key/value pair elements to the hashtable. Even, the hashtable will allow us to add null values. 

 

In c#, the hashtable will use the boxing and unboxing process to store and retrieve the values. To learn more about hashtable, check this c# tutorial topic Hashtable in c# with examples.

Dictionary in C#

In c#, the functionality of the dictionary is same hashtable but the only difference is dictionary will allow only strongly-typed or same datatype elements.

Syntax of Dictionary in C#

Following is the syntax of defining the dictionary in c#.

 

Dictionary<TKey, TValue> dct = new Dictionary<TKey, TValue>();

If you observe the dictionary syntax, we defined the dictionary variable (dct) with an instance of a new dictionary class using type parameters (TKey, TValue) as a placeholder with angle (<>) brackets.

C# Dictionary Example

Following is the example of defining and adding elements to the dictionary in c#.

 

Dictionary<int, string> dct = new Dictionary<int, string>();
dct.Add(1, "Suresh");
dct.Add(4, "Rohini");
dct.Add(2, "Trishi");
dct.Add(3, null);

If you observe the dictionary example, we are able to add only the defined key/value pair elements. To learn more about the dictionary object, check this c# tutorial topic Dictionary in C# with Examples.

C# Hashtable and Dictionary Differences

As discussed, in c# both hashtable and dictionary objects are useful to store the collection of key/value pairs of same or different datatype elements.

 

Following are some of the key differences between hashtable and dictionary in c#.

 

HashTableDictionary
Hashtable is a non-generic collection type and it is available with System.Collections namespace. The dictionary is a generic collection type and it is available with System.Collections.Generic namespace.
Hashtable is not type-safe as it will allow to store the elements of different data types. The dictionary is type-safe as it will allow storing only the elements of the defined type.
By using the DictionaryEntry property, we can get the values from the hashtable. By using the KeyValuePair property, we can get the values from the dictionary.
Hashtable performance is lower because it will use the boxing and unboxing process to store and retrieve the values. Accessing the elements from Dictionary is faster as it is a strongly-typed object.
Hashtable will throw an exception if we try to find a key that does not exist. The dictionary will return null in case the defined key does not exist.
The order of hashtable elements will vary because they were organized based on the hash code of the key. The order of dictionary elements will be same as they were added.

 If you want to store the key/value pairs of different datatypes, you can use a hashtable. If you need type safety and better performance, then you can use a dictionary. To know more about dictionary and hashtable visit our c# tutorial.