Open In App

Difference between Hashtable and Dictionary in C#

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Dictionary is a generic collection which is generally used to store key/value pairs. Dictionary is defined under System.Collection.Generics namespace. It is dynamic in nature means the size of the dictionary is growing according to the need. Example: 

CSharp




// C# program to illustrate Dictionary
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Main Method
    static public void Main()
    {
 
        // Creating a dictionary
        // using Dictionary<TKey, TValue> class
        Dictionary<string, string> My_dict =
                    new Dictionary<string, string>();
 
        // Adding key/value pairs in the Dictionary
        // Using Add() method
        My_dict.Add("a.01", "C");
        My_dict.Add("a.02", "C++");
        My_dict.Add("a.03", "C#");
 
        foreach(KeyValuePair<string, string> element in My_dict)
        {
            Console.WriteLine("Key:- {0} and Value:- {1}",
                              element.Key, element.Value);
        }
    }
}


Output:

Key:- a.01 and Value:- C
Key:- a.02 and Value:- C++
Key:- a.03 and Value:- C#

A Hashtable is a collection of key/value pairs that are arranged based on the hash code of the key. Or in other words, a Hashtable is used to create a collection which uses a hash table for storage. It is the non-generic type of collection which is defined in System.Collections namespace. In Hashtable, key objects must be immutable as long as they are used as keys in the Hashtable. Example: 

CSharp




// C# program to illustrate a hashtable
using System;
using System.Collections;
 
class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Create a hashtable
        // Using Hashtable class
        Hashtable my_hashtable = new Hashtable();
 
        // Adding key/value pair in the hashtable
        // Using Add() method
        my_hashtable.Add("A1", "Welcome");
        my_hashtable.Add("A2", "to");
        my_hashtable.Add("A3", "GeeksforGeeks");
 
        foreach(DictionaryEntry element in my_hashtable)
        {
            Console.WriteLine("Key:- {0} and Value:- {1} ",
                               element.Key, element.Value);
        }
    }
}


Output:

Key:- A3 and Value:- GeeksforGeeks 
Key:- A2 and Value:- to 
Key:- A1 and Value:- Welcome 

Hashtable Vs Dictionary

.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; } 

Hashtable Dictionary
A Hashtable is a non-generic collection. A Dictionary is a generic collection.
Hashtable is defined under System.Collections namespace. Dictionary is defined under System.Collections.Generic namespace.

It doesn’t maintain the order of stored values.

It always maintain the order of stored values.

In Hashtable, there is no need to specify the type of the key and value. In Dictionary, you must specify the type of key and value.
The data retrieval is slower than Dictionary due to boxing/ unboxing. The data retrieval is faster than Hashtable due to no boxing/ unboxing.
In Hashtable, if you try to access a key that doesn’t present in the given Hashtable, then it will give null values. In Dictionary, if you try to access a key that doesn’t present in the given Dictionary, then it will give error.
It is thread safe. It is also thread safe but only for public static members.



Last Updated : 01 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads