---
title: "HashTable in C sharp"  
description: "Hashtable in C# represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can"  
author: "Anonymous User"  
published: 2010-07-27  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/62/hashtable-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# HashTable in C sharp

Hashtable in C# represents a collection of key/value pairs which maps keys to value.

Any non-null object can be used as a key but a value can. We can retrieve items

from hashTable to provide the key. Both keys and values are Objects.

The commonly used functions in Hashtable are:

- Add : To add a pair of value in HashTable
- ContainsKey : Check if a specified key exist or not
- ContainsValue : Check the specified Value exist in HashTable
- Remove : Remove the specified Key and corresponding Value

##### Add : To add a pair of value in HashTable

Syntax : HashTable.Add(Key,Value)

Key : The Key value

Value : The value of corresponding key

Hashtable ht;

ht.Add("1", "Sunday");

##### ContainsKey : Check if a specified key exist or not

Synatx : bool HashTable.ContainsKey(key)

Key : The Key value for search in HahTable

Returns : return true if item exist else false

ht.Contains("1");

##### ContainsValue : Check the specified Value exist in HashTable

Synatx : bool HashTable.ContainsValue(Value)

Value : Search the specified Value in HashTable

Returns : return true if item exist else false

ht.ContainsValue("Sunday")

##### Remove : Remove the specified Key and corresponding Value

Syntax : HashTable.Remove(Key)

Key : The key of the element to remove

ht.Remove("1");

##### Code

```
  Hashtable ht = new Hashtable();        //creating hashtable        int c = 0; //for key        public frmHashTable()        {            InitializeComponent();        }         private void btnAdd_Click(object sender, EventArgs e)        {            if (txtDisplay.Text != "")            {                c++;                ht.Add(c,txtDisplay.Text);              //adding value to hashtable.            }            txtDisplay.Text = "";        }         private void btnDisplay_Click(object sender, EventArgs e)        {            int x;            x= Convert.ToInt32(txtDisplay.Text);            //converting text box text to integer            if (ht.ContainsKey(x))           //checking whether hastable contains particular key or not.                MessageBox.Show(ht[x].ToString());            txtDisplay.Text= "";        }         private void btnsearch_Click(object sender, EventArgs e)        {            if (ht.ContainsKey(Convert.ToInt32(txtDisplay.Text)))                MessageBox.Show("Find");            else                MessageBox.Show("Key not found");            txtDisplay.Text= "";        }         private void btnRemove_Click(object sender, EventArgs e)        {            int x;            x= Convert.ToInt32(txtDisplay.Text);            if (ht.ContainsKey(x))            {                ht.Remove(x); //removing value from hashtable.                MessageBox.Show("Deleted");            }            else                MessageBox.Show("Key not found");            txtDisplay.Text= "";        }
```

##### Screen Shot

![HashTable in C sharp](https://www.mindstick.com/mindstickarticle/e54db9d2-181f-4beb-a986-6f969ff3bd8d/images/037fe254-3235-4012-8257-1fdc3e796742.png)

---

Original Source: https://www.mindstick.com/articles/62/hashtable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
