---
title: "Hash Table in C#"  
description: "In this blog, I’m trying to explain the concept of hash table in c#.  The Hashtable class represents a collection ofkey-and-value pairsthat are organi"  
author: "Sumit Kesarwani"  
published: 2013-05-18  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/515/hash-table-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Hash Table in C#

In this blog, I’m trying to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of hash [table in c#](https://www.mindstick.com/forum/159584/what-are-the-differences-between-a-hashmap-and-a-hash-table-in-c-sharp).\

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 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 hash table

```
Hashtable ht =new Hashtable();This code will create hash table with named ht.
```

##### Adding items to hash table

ht.Add(“001”,”Sachin”);

ht.Add(“002”,”Rahul”);

This code will [add items](https://www.mindstick.com/forum/160381/how-to-add-items-to-a-list-collection-in-c-sharp) to hash table, it uses add() method and has key and value parameters.

##### Retrieving an item value in hash table

string name=ht[001].ToString();

This code will [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) the item value.

##### Removing item from hash table

ht.Remove(“001”);

This code will remove the item from the hash table with key value 001.

##### Example

```
using System;using System.Collections;namespace HashTableConsoleApplication{    class Program    {        static void Main(string[] args)        {            Hashtable ht = new Hashtable();            Console.Write("Enter Maximum Number Of Record To Be In Hashtable = ");            int max = Convert.ToInt32(Console.ReadLine());             for (int i = 1; i <= max; i++)            {                Console.Write("Enter Data =");                string data = Console.ReadLine();                ht.Add(i, data);            }            ICollection key = ht.Keys;            Console.WriteLine("\n\nData in Hashtable");            foreach (int k in key)            {                Console.WriteLine(k + ":" + ht[k]);            }        }    }}
```

##### Output

![Hash Table in C#](https://www.mindstick.com/blogs/39418de7-afee-4905-bdb2-42fb6582abcb/images/e114bfad-c2bb-4f5e-8bc1-72e2a82c2b07.jpg)

---

Original Source: https://www.mindstick.com/blog/515/hash-table-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
