Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples

What Is a Hashtable in C#?

A Hashtable is a non‑generic collection that stores data as key‑value pairs. Unlike arrays or lists that hold a single value per index, a hashtable allows you to retrieve a value directly using its unique key, providing O(1) average‑time lookup.

Typical entries look like this:

{ "001", ".Net" }
{ "002", "C#" }
{ "003", "ASP.Net" }

Here, the keys are "001", "002", and "003", and the corresponding values are ".Net", "C#", and "ASP.Net".

Below we dive into the most common operations you’ll use with Hashtable.

Declaring a Hashtable

Instantiate a hashtable using the Hashtable type and the new keyword:

Hashtable ht = new Hashtable();

Adding Elements

Use the Add method to insert a key‑value pair. Both the key and the value must be supplied:

ht.Add("001", ".Net");
ht.Add("002", "C#");
ht.Add("003", "ASP.Net");

Example 1 – Displaying All Values

The following console application demonstrates creating a hashtable, populating it, and printing each value:

C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples

using System;
using System.Collections;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add("001", ".Net");
            ht.Add("002", "C#");
            ht.Add("003", "ASP.Net");

            ICollection keys = ht.Keys;
            foreach (String k in keys)
            {
                Console.WriteLine(ht[k]);
            }
            Console.ReadKey();
        }
    }
}

Explanation

  1. Declare the Hashtable variable ht.
  2. Add key‑value pairs using Add.
  3. Retrieve the collection of keys via ht.Keys (returns an ICollection).
  4. Iterate over the keys and output the associated value with ht[key].

Running the program outputs:

C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples

Common Methods

ContainsKey

Check if a specific key exists:

ht.ContainsKey("001"); // returns true if key is present

ContainsValue

Verify whether a particular value is stored in the hashtable:

ht.ContainsValue("C#"); // returns true if value is found

Example 2 – Using ContainsKey & ContainsValue

Below is a concise program that demonstrates both methods:

C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples

using System;
using System.Collections;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add("001", ".Net");
            ht.Add("002", "C#");
            ht.Add("003", "ASP.Net");

            Console.WriteLine(ht.ContainsKey("001"));   // true
            Console.WriteLine(ht.ContainsValue("C#"));  // true
            Console.ReadKey();
        }
    }
}

Explanation

  1. Using ContainsKey confirms the presence of "001".
  2. Using ContainsValue confirms that "C#" is stored.

The console displays:

C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples

Key Takeaways

A Hashtable stores elements as key‑value pairs, enabling fast lookup when you know the key. While generic Dictionary<TKey, TValue> is recommended for type safety in modern C#, understanding Hashtable remains valuable for legacy code and interoperability scenarios.

C Language

  1. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  2. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  3. Mastering C++ Char Data Types: Declaration, ASCII, and Conversion Techniques
  4. C++ Operator Overloading – A Practical Guide with Code Examples
  5. C++ Functions Explained with Practical Code Examples
  6. Mastering C# Variables & Operators: Practical Examples & Explanations
  7. Master C# Classes & Objects: A Practical Tutorial with Real‑World Examples
  8. Understanding C# Access Modifiers (Specifiers) with Practical Examples
  9. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  10. Understanding Switch‑Case in C: Syntax, Examples, and Best Practices