---
title: "Difference between Hashtable and Dictionary"  
description: "Difference between Hashtable and Dictionary"  
author: "Sandra Emily"  
published: 2024-06-13  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160743/difference-between-hashtable-and-dictionary  
category: "c#"  
tags: ["c#", "hashtable", "collection", "dictionary"]  
reading_time: 3 minutes  

---

# Difference between Hashtable and Dictionary

[Difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between Hashtable and [Dictionary](https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp)

## Replies

### Reply by Ashutosh Patel

## Dictionary vs Hashtable in C#

In C#, **Hashtable** and **Dictionary** are used to store **key-value pairs**, but they have some differences in terms of functionality, implementation and features,

#### Dictionary

There are several points in Dictionary that make it different from Hashtable which are as follows,

**Namespace-** Dictionary is present in the `System.Collections.Generic` namespace.

**Type Safety-** The `Dictionary`is type-safe, you must specify the **data type** for both the **keys** and the **values** ​​when you declare it.

**Performance-** `Dictionary`usually provides better performance than `Hashtable`since it is a generic collection and avoids boxing/unboxing overhead

**Null Keys-** the `Dictionary`does not allow the **null key** but allows **null values.**

**Enumeration-** Enumerating over a `dictionary`with **foreach** is more efficient than enumerating over a `hashtable`.

**Enumeration Order-** `Dictionary`does not guarantee the order of the elements while `Hashtable`also does not preserve the order of the elements.

**Obsolete-** The `dictionary`is a newer and preferred alternative to **key-value** pairs in most cases.

## Example-

```cs
using System;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
       // Creating a Dictionary
       Dictionary<string, string> dictionary = new Dictionary<string, string>();
       // Adding key-value pairs to the Dictionary
       dictionary.Add("1", "One");
       dictionary.Add("2", "Two");
       dictionary.Add("3", "Three");
       // Accessing elements in the Dictionary
       Console.WriteLine("Value at key '1': " + dictionary["1"]);
       Console.WriteLine("Value at key '2': " + dictionary["2"]);
       Console.WriteLine("Value at key '3': " + dictionary["3"]);
       // Iterate over dictionary
       Console.WriteLine("\n Iterate over dictionary:");
       foreach (KeyValuePair<string, string> pair in dictionary)
       {
           Console.WriteLine("Key: " + pair.Key + ", Value: " + pair.Value);
       }
       Console.ReadLine();
   }
}
```

## Output-

```plaintext
Value at key '1': One
Value at key '2': Two
Value at key '3': Three

Iterate over dictionary:
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
```

#### Hashtable

There are also some points in Hashtable that differ from Dictionary which are as follow,

**Namespace-** Dictionary is present in the `System.Collections` namespace.

**Type Safety-** The `Hashtable`is not type-safe, because it allows any **datatype** for both **key** and **value**.

**Performance-** `Hashtable` uses a simple **hash code** algorithm that's why its performance is not as good as Dictionary

**Null Keys-** The `Hashtable` allows both the **null key** and **null value**

**Enumeration-** Hashtable needs **boxing/unboxing**, which can degrade performance during calculation.

**Enumeration Order-** The `Hashtable`doesn't preserve the order of the elements.

**Obsolete-** The Hashtable is a part of an older **non-generic collection** framework in C#

## Example-

```cs
using System;
using System.Collections;
class Program
{
   static void Main()
   {
       // Creating a Hashtable
       Hashtable hashtable = new Hashtable();
       // Adding key-value pairs to the Hashtable
       hashtable.Add("1", "One");
       hashtable.Add("2", "Two");
       hashtable.Add("3", "Three");
       // Accessing elements in the Hashtable
       Console.WriteLine("Value at key '1': " + hashtable["1"]);
       Console.WriteLine("Value at key '2': " + hashtable["2"]);
       Console.WriteLine("Value at key '3': " + hashtable["3"]);
       // Iterating through the Hashtable
       Console.WriteLine("\nIterating through the Hashtable:");
       foreach (DictionaryEntry entry in hashtable)
       {
           Console.WriteLine("Key: " + entry.Key + ", Value: " + entry.Value);
       }
   }
}
```

## Output-

```plaintext
Value at key '1': One
Value at key '2': Two
Value at key '3': Three

Iterating over the Hashtable:
Key: 2, Value: Two
Key: 3, Value: Three
Key: 1, Value: One
```

**Also, Read:** [Pascal Triangle](https://www.mindstick.com/forum/160731/pascal-triangle)


---

Original Source: https://www.mindstick.com/forum/160743/difference-between-hashtable-and-dictionary

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
