Differencce between HashTable and Dictionary in C#

Shahzad Aslam
3 min readApr 10, 2024

Hashtable is related to System.Collections namespace. Hashtable is used to store key-value pairs of any type (int, float, string etc.). Dictionary is related to System.Collections.Generic namespace. Dictionary is used to store key-value pairs of specific type (int, float, string etc.).

1. Hashtable:

1. Thread Safety:

Hashtable is not inherently thread-safe. If multiple threads access a Hashtable concurrently, and at least one of the threads modifies the Hashtable, it must be synchronized externally.

2. Null Keys and Values:

Hashtable allows both null keys and null values.If a key has null value it does not throw exception

3. Performance:

Hashtable is part of the older collections in .NET, and it is generally less performant than newer collections like Dictionary.

It uses a mechanism called hashing to locate elements by key, which involves calculating a hash code for each key.

4. Type Safety:

Hashtable is not type-safe. It treats all keys and values as objects, which mean you can store any type of value in hashtable and type casting is required when retrieving values.

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.