Hashtable Class

Represents a collection of key/value pairs that are organized by the hash code of the key. This class cannot be inherited.

Overview

The Hashtable class implements a collection of key/value pairs that are organized based on the hash code of the key. It provides efficient insertion, lookup, and deletion operations, making it suitable for scenarios where quick access to values using their keys is required. Hashtable uses hashing to map keys to buckets, which significantly speeds up these operations.

This class is part of the System.Collections namespace.

When to use Hashtable

Thread Safety

A public static (Shared in Visual Basic) member of this type is thread-safe. Any instance member is not guaranteed to be thread-safe.

Syntax

Declaration

[SerializableAttribute]
public sealed class Hashtable : IDictionary, ICollection, IEnumerable, ICloneable

This indicates that the Hashtable class is serializable, public, and sealed, and it implements the IDictionary, ICollection, IEnumerable, and ICloneable interfaces.

Namespace

System.Collections

Assembly

mscorlib.dll

Members

Inheritance

Object
Hashtable
IDictionary
ICollection
IEnumerable
ICloneable

Hashtable inherits from Object and implements IDictionary, ICollection, IEnumerable, and ICloneable.

Remarks

The Hashtable class provides a collection that stores key-value pairs. Keys must be unique. If you try to add an element with a key that already exists, the existing element is not overwritten.

The Hashtable uses the GetHashCode method of the key to determine the bucket in which the key/value pair is stored. The Equals method is used to compare keys within a bucket.

By default, Hashtable uses case-sensitive, ordinal comparison for keys. You can specify a case-insensitive or a custom comparer by passing an appropriate IEqualityComparer to the constructor.

Hashtable is not synchronized, meaning it is not thread-safe. If you need a thread-safe collection, consider using ConcurrentDictionary<TKey, TValue> or creating a thread-safe wrapper around Hashtable.

Examples

The following example demonstrates how to create, populate, and iterate through a Hashtable.


using System;
using System.Collections;

public class HashtableExample
{
    public static void Main(string[] args)
    {
        // Create a new Hashtable
        Hashtable myHashtable = new Hashtable();

        // Add key-value pairs
        myHashtable.Add("apple", "A fruit");
        myHashtable.Add("banana", "A yellow fruit");
        myHashtable.Add("carrot", "An orange vegetable");

        // Access a value by key
        Console.WriteLine($"Value for 'apple': {myHashtable["apple"]}");

        // Check if a key exists
        if (myHashtable.ContainsKey("banana"))
        {
            Console.WriteLine("Hashtable contains the key 'banana'.");
        }

        // Iterate through the Hashtable
        Console.WriteLine("\nIterating through the Hashtable:");
        foreach (DictionaryEntry entry in myHashtable)
        {
            Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
        }

        // Remove an element
        myHashtable.Remove("carrot");
        Console.WriteLine("\nAfter removing 'carrot':");
        foreach (DictionaryEntry entry in myHashtable)
        {
            Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
        }

        // Clear the Hashtable
        myHashtable.Clear();
        Console.WriteLine($"\nHashtable count after clearing: {myHashtable.Count}");
    }
}