---
title: "Hashtable in C#"  
description: "How to use a Hashtable in C#."  
author: "Uttam Misra"  
published: 2012-05-18  
updated: 2020-08-11  
canonical: https://www.mindstick.com/blog/306/hashtable-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Hashtable in C#

How to use a [Hashtable in C#](https://www.mindstick.com/forum/33627/how-to-use-hashtable-in-c-sharp-dot-net).

##### Creating a Hashtable

The following [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) creates a Hashtable:

```
private Hashtable hsTable = new Hashtable();
```

##### Adding Items to a Hashtable

##### Add method of Hashtable is used to add items to the hashtable. The

##### method has index and value parameters.

The following code adds [items](https://www.mindstick.com/articles/44359/store-your-valuable-items-in-a-mini-storage-unit) to hashtable.

```
hsTable .Add("Game1",  "Cricket");hsTable.Add("Game2",  "Football");hsTable.Add("Game3",  "Badminton");hsTable.Add("Game3",  "Basketball");hsTable.Add("Game3",  "Tennis");
```

##### Retrieving an Item Value from Hashtable

The following code returns the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of "Cricket" key:

[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) name = hsTable ["Cricket "].ToString();

##### Removing Items from a Hashtable

The [Remove method](https://answers.mindstick.com/qa/93808/why-are-use-the-remove-method-in-jquery) removes an item from a Hashtable.

The following code removes item with [index](https://www.mindstick.com/blog/198/index-in-sql-server) "Cricket" from the hashtable:

hsTable.Remove("Cricket");

##### Looking through all Items of a Hashtable

The following code [loops](https://www.mindstick.com/forum/161927/explain-the-python-while-loops-with-example) through all items of a hashtable and reads the values. \
\

```
IDictionaryEnumerator en = hsTable.GetEnumerator();
while (en.MoveNext()){      string str = en.Value.ToString(); }
```

---

Original Source: https://www.mindstick.com/blog/306/hashtable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
