Hash Table In C#

What is a hash table ? When do we use it and what is the syntax?

In C#, hash table is nothing but a collection of key or values, which maps a key to the value. Any non-null object can be used as a key. We can retrieve the items from hash table to provide the key.

Let see what are the common functions used in hash table, listed below.

  • Add- To add a pair of value in Hashtable
  • Containskey- It checks if a specified key exists or not.
  • ContainsValue- It checks if a specified value exists or not.
  • Remove- Deletes the specified key and value.

Now, let’s see how to use each function in the hash table in detail.

  1. Add
    This function is used to add a pair of values in the hash table.

    Syntax
    1. HashTable. Add(Key,Value)  
    2. Hashtable ht;  
    3. ht.Add("1","Sunday");  
  2. ContainsKey
    This function checks whether a specified key exists or not.

    Syntax
    1. bool HashTable. Containskey (Key)  
    2. ht.Contains("1");  
  3. ContainsValue
    This function checks whether the specified value exists in hash table or not.

    Syntax
    1. bool HashTable. ContainsValue(Value)  
    2. ht.ContainsValue ("Sunday"); 
  4. Remove
    This function will delete the specified key and the corresponding values.

    Syntax
    1. HashTable.Remove(key)  
    2. ht.Remove ("1");  

Sharing is caring.

Ebook Download
View all
Learn
View all