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.
- Add
This function is used to add a pair of values in the hash table.
Syntax
- HashTable. Add(Key,Value)
- Hashtable ht;
- ht.Add("1","Sunday");
- ContainsKey
This function checks whether a specified key exists or not.
Syntax
- bool HashTable. Containskey (Key)
- ht.Contains("1");
- ContainsValue
This function checks whether the specified value exists in hash table or not.
Syntax
- bool HashTable. ContainsValue(Value)
- ht.ContainsValue ("Sunday");
- Remove
This function will delete the specified key and the corresponding values.
Syntax
- HashTable.Remove(key)
- ht.Remove ("1");
Sharing is caring.