Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A hashtable is a data structure that is a bucket of the array which keeps the key/value pairs in them. It accesses the elements in the collection using the key. You utilize a hash table when you need to access items by key and can determine a practical key value. Without further ado, let's dive into the main topic. In this article, we shall be covering the C#Hashtable Class. We will discuss the constructors, properties methods, and code examples with their output and explanation.
Hashtable Class
The Hashtable class is a collection of key/value pairs structured using the key's hash code. System.Collections namespace contains this class. The Hashtable class has several methods that may be used to execute various operations on hashtables. Keys are used in Hashtable to access the elements in the collection. On a 64-bit machine, you may boost the total capacity to 2 billion items for massive Hashtable objects.
Characteristics of Hashtable Class
The key of a Hashtable cannot be null, but still, the value can.
As long as they are utilized as keys in the Hashtable, key objects must be immutable.
The amount of items that a Hashtable can contain is known as its capacity.
Every key object in the Hashtable has a hash function.
Values may be obtained by passing the associated key in the indexer, such as myHashtable[key].
DictionaryEntry objects are used to store elements.
Constructors for Hashtable Class
Properties for Hashtable Class
Methods for Hashtable Class
Example
Let's go through some coding examples so that you can an overview of the implementation of the C# hashtable class.
Code 1
using System;
using System.Collections;
class DemoHashtable {
static void Main(string[] args)
{
Hashtable g = new Hashtable();
g.Add("1", "Welcome");
g.Add("2", "to");
g.Add("3", "Coding");
g.Add("4", "Nin");
g.Add("5", "jas");
ICollection key = g.Keys;
Console.WriteLine("Hashtable Contains:");
foreach(var val in key)
{
Console.WriteLine(val + "-" + g[val]);
}
Console.WriteLine();
g.Remove("2");
Console.WriteLine("Hashtable after removing element 2:");
foreach(var val in key)
{
Console.WriteLine(val + "-" + g[val]);
}
}
}
Output
Hashtable Contains:
3-Coding
5-jas
4-Nin
2-to
1-Welcome
Hashtable after removing element 2:
3-Coding
5-jas
4-Nin
1-Welcome
In this example, we have created a Hashtable. After creating them, we have entered the values in them. After entering them, we deleted some values and printed the output.
Code 2
using System;
using System.Collections;
namespace CollectionsApplication {
class ProgramHashDemo {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
ht.Add("001", "Mohammad Saalim");
ht.Add("002", "Mutiur Rehman");
ht.Add("003", "Atif Rashid");
ht.Add("004", "Mohammad Yawar");
ht.Add("005", "Paritosh Singh");
ht.Add("006", "Firdausia Fatima");
ht.Add("007", "Mohammad Afzal");
if (ht.ContainsValue("Nuha Ali")) {
Console.WriteLine("This Student’s name is already on the list");
} else {
ht.Add("008", "Kaif Ali");
}
ICollection key = ht.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + ht[k]);
}
Console.ReadKey();
}
}
}
Output
This Student's name is already on the list
007: Mohammad Afzal
004: Mohammad Yawar
005: Paritosh Singh
002: Mutiur Rehman
003: Atif Rashid
001: Mohammad Saalim
006: Firdausia Fatima
In this example, we have created a Hashtable after creating them. We entered some values in it. We checked for specific values. Suppose it is present in it. We printed the output.
It is a data structure that helps store elements in key-value pairs, where value is data related to keys.
What is the application of hashed key and value in Hashtable?
We provide an object to use as a key and the value we associate with that key in Hashtable. The hash code is used as the index, and the key is hashed for storing the data within the table.
What is a Hashtable collection?
Key-value pairs are kept in the Hashtable collection. It improves search by computing each key's hash code and keeping it in a separate bucket internally, then matching the hash code of the supplied key when accessing data.
Conclusion
In this article, we have looked into the Hashtable class of C#. We have discussed the characteristics and their constructors, properties, and methods. We also have addressed the code examples along with their output and explanation.