Table of contents
1.
Introduction
2.
Hashtable Class
3.
Characteristics of Hashtable Class
4.
Constructors for Hashtable Class
5.
Properties for Hashtable Class
6.
Methods for Hashtable Class
7.
Example
7.1.
Code 1
7.2.
Code 2
8.
Frequently Asked Questions
8.1.
What is a Hashtable?
8.2.
What is the application of hashed key and value in Hashtable?
8.3.
What is a Hashtable collection?
9.
Conclusion
Last Updated: Mar 27, 2024

C# Hashtable Class

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

You can also read about the topic hash function in data structure, singleton design pattern in c#, and Ienumerable vs Iqueryable

Frequently Asked Questions

What is a Hashtable?

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.

You can improve your skills in  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you're just getting started to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you in enhancing your knowledge. 

"Happy Coding!"

Live masterclass