In c#, Hashtable is used to store a collection of key/value pairs of different data types, and those are organized based on the hash code of the key.
Generally, the hashtable object will contain buckets to store elements of the collection. Here, the bucket is a virtual subgroup of elements within the hashtable, and each bucket is associated with a hash code, which is generated based on the key of an element.
In c#, the hashtable is same as a dictionary object, but the only difference is the dictionary object is used to store a key-value pair of the same data type elements.
When compared with dictionary objects, the hashtable will provide a lower performance because the hashtable elements are of object type, so the boxing and unboxing process will occur when we store or retrieve values from the hashtable.
In c#, hashtable is a non-generic type of collection to store a key/value pair elements of different data types, and it is provided by System.Collections namespace.
As discussed, the collection is a class, so to define a hashtable, you need to declare an instance of the hashtable class before we perform any operations like add, delete, etc. as shown below.
If you observe the above hashtable declaration, we created a new hashtable (hshtbl) with an instance of hashtable class without specifying any size.
The following are some of the commonly used properties of hashtable in the c# programming language.
Property | Description |
---|---|
Count | It is used to get the number of key/value pair elements in the hashtable. |
IsFixedSize | It is used to get a value to indicate whether the hashtable has a fixed size or not. |
IsReadOnly | It is used to get a value to indicate whether the hashtable is read-only or not. |
Item | It is used to get or set the value associated with the specified key. |
IsSynchronized | It is used to get a value to indicate whether access to the hashtable is synchronized (thread-safe) or not. |
Keys | It is used to get the collection of keys in the hashtable. |
Values | It is used to get the collection of values in the hashtable. |
The following are some of the commonly used methods of hashtable to perform operations like add, delete, etc., on elements of hashtable in c# programming language.
Method | Description |
---|---|
Add | It is used to add an element with a specified key and value in a hashtable. |
Clear | It will remove all the elements from the hashtable. |
Clone | It will create a shallow copy of the hashtable. |
Contains | It is useful to determine whether the hashtable contains a specific key or not. |
ContainsKey | It is useful to determine whether the hashtable contains a specific key or not. |
ContainsValue | It is useful to determine whether the hashtable contains a specific value or not. |
Remove | It is useful to remove an element with a specified key from the hashtable. |
GetHash | It is useful to get the hash code for the specified key. |
In c#, while adding elements to the hashtable, you need to make sure that there will not be any duplicate keys because the hashtable will allow us to store duplicate values, but keys must be unique to identify the values in the hashtable.
In the hashtable, we can add key/value pair elements of different data types. Following is the example of adding elements to the hashtable in c#.
If you observe the above example, we created a new hashtable (htbl) and added different data type elements (key/value) to the hashtable (htbl) in different ways. As discussed, Add method will throw an exception if we try to add a key (2) that is already existing to handle that exception; we used a try-catch block.
When you execute the above c# program, we will get the result below.
If you observe the above result, we got an exception when we tried to add a key (2) that is already existing and added a key (3) that is not existing in the hashtable.
In c#, we can also assign key/value pairs to hashtable at the time of initialization as shown below.
In c#, we have different ways to access hashtable elements, i.e., either using keys or by iterating through a hashtable using a foreach loop.
Following is an example of accessing hashtable elements in different ways.
If you observe the above example, we access hashtable elements in different ways by using keys, foreach loop, and available properties (Keys, Values) based on our requirements.
When you execute the above c# program, we will get the result below.
In c#, you can delete elements from the hashtable by using the Remove() method. Following is the example of deleting elements from the hashtable in the c# programming language.
If you observe the above example, we used a Remove() method to delete a particular value of the element from the hashtable using keys.
When you execute the above c# program, we will get the result below.
If you want to remove all the elements from a hashtable, then use the Clear() method.
By using Contains(), ContainsKey(), and ContainsValue() methods, we can check whether the specified element exists in the hashtable or not. In case it exists, these methods will return true otherwise false.
Following is the example of using Contains(), ContainsKey(), and ContainsValue() methods to check for an item that exists in a hashtable or not in c#.
If you observe the above example, we used Contains(), ContainsKey(), and ContainsValue() methods to check for particular keys and values that exist in a hashtable (htbl) or not.
When you execute the above c# program, we will get the result below.
The following are the important points that need to remember about hashtable in c#.