Hello readers, In this tutorial, we are going to learn Hashtable in C#. Hashtable is a collection similar to Generic Dictionary collection. It stores the value in form of key-value pairs.
What is Hashtable?
Hashtable is a collection similar to Generic Dictionary collection. It stores the value in form of key-value pairs. The Hashtable is a weekly typed data structure that we can add keys and values of any type to the Hashtable.
Some of the properties of Hashtable.
1- Count: This property is used to get the total count of key/value pairs in the Hashtable.
2- IsReadOnly: This method gets a boolean value which indicates whether the Hashtable is read-only or not.
3- Item: This property is used to get or set the value associated with the key in the hashtable.
4- Keys: Gets an ICollection of keys in the hashtable
5- Values: Get an ICollection of values in the Hashtable.
Methods of Hashtable :
1- Add: Adds an item into the hastable with the key value.
2- Remove: This method is used to remove an item with the specified key from the hashtable.
3- Clear: Removes all the items from the hashtable.
4- Contains: This method is used to Checks whether the hashtable contains a specific key.
5- ContainsKey: Checks whether the hashtable contains a specific key.
6- ContainsValue: Checks whether the hashtable contains a specific value.
7- GetHash: Returns the hash code for the specified key.
Add key-value into Hashtable:
The Add() method adds an item with a key and value into the Hashtable. Key and value can be of any data type. Key cannot be null whereas value can be null.
Example:
Hashtable ht = new Hashtable();
ht.Add(1, "C#"); ht.Add(2, "ASP.NET"); ht.Add(3, "ASP.NET MVC"); ht.Add(4, "PHP"); ht.Add(5, "SQL");
Assign value to Hashtable at the time of Initialization:
Hashtable ht = new Hashtable() { { 1, "C#" }, { 2, "ASP.NET" }, { 3, "ASP.NET MVC" }, { 4, "PHO" }, { 5, "SQL"} };
Add Value to Hashtable using Dictionary:
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "C#"); dict.Add(2, "ASP..NET"); dict.Add(3, "ASP.,NET MVC"); Hashtable ht = new Hashtable(dict);
Retrieve a value from Hashtable:
You can retrieve the value of an existing key from the Hashtable using the indexer.
Hashtable ht = new Hashtable(); ht.Add(1, "C#"); ht.Add(2, "ASP.NET"); ht.Add(3, "ASP.NET MVC"); ht.Add(4, "PHP"); ht.Add(5, "SQL"); string strValue1 = (string)ht[2];// returns ASP.NET string strValue2 = (string)ht[4];// returns PHP
Iterate to Hashtable:
Hashtable ht = new Hashtable(); ht.Add(1, "C#"); ht.Add(2, "ASP.NET"); ht.Add(3, "ASP.NET MVC"); ht.Add(4, "PHP"); ht.Add(5, "SQL"); foreach (DictionaryEntry item in ht) Console.WriteLine("key:{0}, value:{1}",item.Key, item.Value);
Remove an Item from the Hashtable:
The Remove() method removes the item with the specified key from the hashtable.
Example:
Hashtable ht = new Hashtable(); ht.Add(1, "C#"); ht.Add(2, "ASP.NET"); ht.Add(3, "ASP.NET MVC"); ht.Add(4, "PHP"); ht.Add(5, "SQL"); ht.Remove(1); // removes (1, "C#");
Check Existence of Element in Hashtable:
We can check the existence of an element using the following method provided by Hashtable.
1- Contains: Is used to check whether the specified key exists in the hastable.
2- ContainsKey: Is also used to check whether the specified key exists in the hastable.
2- ContainsValue: Is also used to check whether the specified value exists in the hastable.
Example:
Hashtable ht = new Hashtable(); ht.Add(1, "C#"); ht.Add(2, "ASP.NET"); ht.Add(3, "ASP.NET MVC"); ht.Add(4, "PHP"); ht.Add(5, "SQL"); ht.Contains(2);// returns true ht.ContainsKey(2);// returns true ht.Contains(7); //returns false ht.ContainsValue("C#"); // returns true
Remove all item from hashtable:
Clear() method removes all the key-value pairs in the Hashtable.
Example:
Hashtable ht = new Hashtable(); ht.Add(1, "C#"); ht.Add(2, "ASP.NET"); ht.Add(3, "ASP.NET MVC"); ht.Add(4, "PHP"); ht.Add(5, "SQL"); ht.Clear(); // removes all elements
View More:
Conclusion:
I hope you understand this tutorial. Your Feedback and suggestions are always welcome to me.
Thank You.