Hashtable is another useful data structure in C# that allow us to store data using key value pairs. let's learn how to use Hashtable in C# with examples.
using System.Collections; Hashtable htObj = new Hashtable();
C# Hashtable is a collection object that comes under System.Collections namespace, Hashtable is similar to generic Dictionary collection object, with some difference like Hashtable allow us to store data of different data type, which is not supported in dictionary object.
Adds an item with a key and value into the hashtable,
below is an example of adding key value in hashtable in C#.
Hashtable htObj = new Hashtable(); htObj.Add(1, "This is string");
Checks whether the hashtable contains a specific key
Removes the item with the specified key from the hashtable
Removes all the items from the hashtable.
Checks if hashtable contains a specific key
Checks if the hashtable contains a specific value.
Gets boolean value indicating whether the Hashtable is read-only.
Gets or sets the value associated with the specified key.
Gets an ICollection of keys in the Hashtable.
Gets an ICollection of values in the Hashtable.
Below is an example of how to get value from hashtable object in C#
Hashtable htObj = new Hashtable(); htObj.Add(1, "This is string"); string strValue1 = (string)htObj[1];
Gets the total count of keys in the Hashtable.
Remember that hashtable works on key/value combination, key or value can be of any data type, look at the example below, this is how we can add key/value to Hashtable
Hashtable htObj = new Hashtable(); htObj.Add(1, "This is string"); htObj.Add(2, 2.2); htObj.Add(3, true); htObj.Add(4, DateTime.Now); htObj.Add(5, null); htObj.Add("Six", "see the key difference");
Now you see how to retrieve value from hashtable, As you have seen that hashtable store values with key, so you can retrieve value using same key, or you can hashtable get value by index.
string strValue1 = (string)htObj[1]; Console.WriteLine(strValue1); DateTime? strValue4 = (DateTime?)htObj[4]; Console.WriteLine(strValue4);
As you have seen that a Hashtable can contains a key and a value of any data type. So while casting, values must be cast to an appropriate data type otherwise it will give compile-time error.
Notice in above example while retrieving data from hashtable, one converted to string and four converted to datetime
Now we have seen that in hashtable we can have different data type for key and values both field, but ideally we should have only same data type for key fields. Now let's try to loop through a hashtable c#, all the key values of above hashtable dynamically, htObj.Keys will get all the keys.
foreach (int key in htObj.Keys) { Console.WriteLine("Key: " + key + " Value: " + htObj[key]); }
See the error for last key, which is a different data type, when all keys are integer and the last (six) key is a string
So, remember to keep all the keys of same data type while creating a hashtable in C#
Or, instead of strongly type, we could have written var, like foreach (var key in htObj.Keys)
You should also look at others C# Data Structures Fundamental.