C# Hashtable vs Dictionary: When should you use a Hashtable or a Dictionary?

C# Hashtable vs Dictionary
C# Hashtable vs Dictionary

C#|Hashtable vs Dictionary

Sr.No.C# HashtableC# Dictionary
1.Hashtable is a non-generic typeDictionary is a generic type of collection
2.Values in Hashtable need boxing/unboxing.In Dictionary, there is no need for boxing/unboxing.
3.Hashtable is defined in System.Collections namespace.Dictionary is defined in System.Collections.Generic namespace.
4.A Hashtable is a weakly typed (non-generic) collection, this means it stores key-value pairs of any data type.In Dictionary, you can store key/value pairs of specific data types.
5.Hashtable returns null when trying to find a key that does not exist.The dictionary will throw an exception if you try to find a key that doesn’t exist.
6.In Hashtable, the data retrieval is slower than in Dictionary due to boxing and unboxing.The Dictionary collection is faster than Hashtable since there is no boxing and unboxing.
7.Hashtable never maintains the order of stored values.A dictionary maintains the order of stored values.
8Hashtable is thread-safe and can be used by multiple reader threads and a single writer thread.A Dictionary can support multiple readers concurrently, as long as the collection doesn’t modify. Although just for public static members, it is also thread-safe.
hashtable vs dictionary c#

C# Hashtable

C# Hashtable: A Hashtable is a collection of key/value pairs. Hashtable is used to store unique key data. If we try to assign value to an already used key, the previously stored information is simply overwritten.
Hashtable is the non-generic type of collection that is defined in the System.Collections namespace.


using System;
using System.Collections;
using System.Linq;
namespace ShekhAli
    public class CSharpHashtableExample
        static void Main(string[] args)
            Hashtable hashtable = new Hashtable();
            hashtable.Add(1, "A");
            hashtable.Add(2, "B");
            hashtable.Add(3, "C");
            hashtable.Add(4, "D");
            hashtable.Add(5, "F");

            Console.WriteLine(hashtable[1]);// Output: A

            foreach (DictionaryEntry element in hashtable)
              Console.WriteLine($"Key: {element.Key} and Value: {element.Value} ");


//Key: 5 and Value: F
//Key: 4 and Value: D
//Key: 3 and Value: C
//Key: 2 and Value: B
//Key: 1 and Value: A

In the above code example, we are taking advantage of the DictionaryEntry class to iterate the elements in a Hashtable.

You can also use the GetEnumerator() method of the Hashtable class to retrieve the key-value pairs stored in the collection. Here is some code that demonstrates this:

IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
 while (enumerator.MoveNext())
    Console.WriteLine($"Key: {Convert.ToString(enumerator.Key)} Value: {Convert.ToString(enumerator.Value)}");

C# Dictionary

In C#, Dictionary is a generic collection that can be used to store key/value pairs. It is defined in  System.Collection.Generics  namespace. Dictionary is dynamic in nature, this means that the size of the dictionary can change dynamically and items can be added or removed from the dictionary as needed.


using System;
using System.Collections.Generic;

public class GenericDictionaryExample
    public static void Main(string[] args)
        // Creating a new dictionary using int keys and string values.
        Dictionary<string, string> myDictionary = new Dictionary<string, string>();

        // Add elements to the dictionary object.
        myDictionary.Add("101", "A");
        myDictionary.Add("102", "B");
        myDictionary.Add("103", "C");

        // Accessing elements from dictionary as KeyValuePair objects.
        foreach (KeyValuePair<string, string> element in myDictionary)
            Console.WriteLine($" Key : {element.Key} and  Value: {element.Value} ");
    // Output:
    // Key : 101 and Value: A
    // Key : 102 and Value: B
    // Key : 103 and Value: C



The primary difference between a hashtable and a dictionary is that a dictionary doesn’t require boxing and unboxing because it is strongly typed whereas a hashtable is a weakly typed collection.
The choice between a Hashtable and a Dictionary depends on whether you need a type-safe collection. The majority of the time, using a dictionary is generally a wise decision as it is an enhanced version of the Hashtable.

FAQs: HashTable Vs Dictionary

Q: Are duplicate keys allowed in a hashtable?

Ans: Duplicate keys are not allowed because hashtables can only contain unique keys.

Q: Is the C# dictionary thread safe?

Ans: Yes, it’s safe if you don’t modify the dictionary anymore. Thread safety is only an issue in reading/writing scenarios.
So, when you are working on a multi-threaded application to add and get items from the dictionary simultaneously. the result may be inaccurate because the Dictionary is not thread-safe.
Therefore, you must implement your own synchronization to allow the collection access by multiple threads for reading and writing.

Q: Is Hashtable faster than the dictionary in C#?

Ans: Dictionary is faster than hashtable as the dictionary is generic and strongly typed. The data retrieval from the Hashtable is slower as it takes an object as its data type which leads to boxing and unboxing.

Q: Which is better Hashtable or dictionary in C#?

Ans: Dictionary is a generic type, therefore if you try to find a key that isn’t there, you will get an error. The Dictionary collection is faster than Hashtable because there is no boxing and unboxing.

Q: Can the values in a hashtable be NULL?

Ans: Hashtable values can be null or duplicated, but keys must be unique and cannot be null.

Q: Can a dictionary have null or duplicate keys?

Ans: In Dictionary, the key cannot be null, but the value can be.
Additionally, the key must be unique; if you attempt to use a key that already exists in the dictionary, an exception will be thrown by the compiler.

References: MSDN-Hashtable and Dictionary Collection

I hope you enjoyed reading this article “C# Hashtable vs Dictionary“. If you find something incorrect or wish to offer more information regarding the subjects discussed here, please leave a comment below.

Articles to Check Out:

Leave a Comment