---
title: "C# Hashtable"  
description: "In this blog I’m explaining about Hashtable class in C#.Hashtable:The Hashtable class represents a collection of key-and-value pairs that are organize"  
author: "Anonymous User"  
published: 2014-09-22  
updated: 2014-09-22  
canonical: https://www.mindstick.com/blog/694/c-sharp-hashtable  
category: ".net"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# C# Hashtable

In this blog I’m explaining about Hashtable [class in C#](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example).

Hashtable:

The Hashtable class represents a [collection](https://www.mindstick.com/articles/1718/collections-in-java) of key-and-value pairs that are [organized](https://yourviews.mindstick.com/story/4668/regions-rivers-where-kumbha-mela-is-organized) based on the hash code of the key. It uses the key to [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) the [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) in the collection.

A [hash table](https://www.mindstick.com/forum/157906/what-is-the-difference-between-a-hash-table-and-an-array-when-would-you-use-one-over-the-other) is used when you need to access elements by using key, and you can [identify](https://www.mindstick.com/forum/159425/java-app-crash-arrayindexoutofboundsexception-identify-invalid-index) a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

\
Creating Hashtable:

The hashtable class in c# represent a Hashtable.

Hashtable hashtable=new Hashtable();

\
**Adding items in Hashtable with method:**

**\**Add method of Hashtable is used to [add items](https://www.mindstick.com/forum/160381/how-to-add-items-to-a-list-collection-in-c-sharp) into the Hashtable. The

\

method has index and value parameters.

\

hashtable.Add(“UP 70”,”Allahabad”);

hashtable.Add(“UP 72”,”PratapGarh”);

hashtable.Add(“UP 66”,”Bhadohi”);

\
**Adding items in Hashtable directly:**

First you can create object of Hashtable, Hashtable has no values we

\

directly assign with the [indexer](https://www.mindstick.com/blog/103/indexer-in-c-sharp) which uses the square brackets.

\

```
Hashtable hashtable=new Hashtable();Hashtable [0]=”Kamlakar”;Hashtable [1]=”Rohit”;Hashtable [4]=”Pawan”;
```

## Retrieving an item value from Hashtable:

## \

the following code returns the value of “UP 70”key.

\

string cityname=hashtable[“UP 70”].ToString();

## Removing items from Hashtable:

Removing items from Hashtable for achieve this task use Remove () method of Hashtable class the following code remove items with particular index from hashtable.

Hashtable.Remove(“UP 70”);

##### Looking all items of Hashtable:

The following code loops through all items of a hashtable and reads the

\

values.

\

\

IDictionaryEnumerator en = hshTable.GetEnumerator();

\

\

```
While(en.MoveNext()){            string st=en.Value.ToString();}          
```

```
using System;using System.Collections; namespace ExHashtable{    class Program    {        static void Main(string[] args)        {            Hashtable hashtable = new Hashtable();            hashtable.Add("UP 70","Allahabad");            hashtable.Add("UP 72","PratapGarh");            hashtable.Add("UP 66","Bhadohi");            if (hashtable.ContainsValue("Banaras"))            {                Console.WriteLine("This student name is already in the list");            }            else            {                hashtable.Add("UP 65", "Banaras");            }            ICollection key = hashtable.Keys;             foreach (string k in key)            {                Console.WriteLine(k + ": " + hashtable[k]);            }            Console.ReadKey();        }    }} 
```

##### Output

![C# Hashtable](https://www.mindstick.com/blogs/63d829f3-33b4-4c04-b854-037809bfb627/images/b97eac4f-0f89-4d37-896d-54f57361e96b.png)

## Example retrieving item value from Hashtable:

```
using System;using System.Collections; namespace ExHashtable{    class Program    {        static void Main(string[] args)        {            Hashtable hashtable = new Hashtable();            hashtable.Add("UP 70","Allahabad");            hashtable.Add("UP 72","PratapGarh");            hashtable.Add("UP 66","Bhadohi");            string cityname=hashtable["UP 70"].ToString();            Console.WriteLine(cityname);        }    }} 
```

##### Output

![C# Hashtable](https://www.mindstick.com/blogs/63d829f3-33b4-4c04-b854-037809bfb627/images/1e41641f-2e76-4fcf-98ac-76c61a91017e.png)

##### Example of removing items from Hashtable:

```
using System;using System.Collections; namespace ExHashtable{    class Program    {        static void Main(string[] args)        {            Hashtable hashtable = new Hashtable();            hashtable.Add("UP 70","Allahabad");            hashtable.Add("UP 72","PratapGarh");            hashtable.Add("UP 66","Bhadohi");            hashtable.Remove("UP 66");            ICollection key = hashtable.Keys;            foreach (string k in key)            {                Console.WriteLine(k + ": " + hashtable[k]);            }            Console.ReadKey();        }    }}
```

##### Output

![C# Hashtable](https://www.mindstick.com/blogs/63d829f3-33b4-4c04-b854-037809bfb627/images/9c765f88-4027-45e3-8639-7c2db3714d8e.png)

in my next post i'll explain about [Bootstrap ContextMenu](https://www.mindstick.com/blog/697/Bootstrap%20ContextMenu#.VPVM3_mUdz8)

---

Original Source: https://www.mindstick.com/blog/694/c-sharp-hashtable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
