.NET Documentation

Hashtable Class

Namespace: System.Collections

Assembly: System.Collections.dll

Summary

Represents a collection of keys and values that are organized based on the hash code of the key. It provides fast lookups and is thread-safe for a single writer with multiple readers.

Syntax

public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable

Constructors

SignatureDescription
Hashtable()Initializes a new empty hashtable with the default load factor and capacity.
Hashtable(int capacity)Initializes a new empty hashtable with the specified initial capacity.
Hashtable(IEqualityComparer? comparer)Initializes a new empty hashtable that uses the specified IEqualityComparer.
Hashtable(IDictionary d)Initializes a new hashtable containing elements copied from the specified dictionary.
Hashtable(IDictionary d, IHashCodeProvider? hcp, IComparer? comparer)Initializes a new hashtable with a custom hash code provider and comparer.

Properties

NameTypeDescription
CountintGets the number of key/value pairs contained in the Hashtable.
IsReadOnlyboolIndicates whether the Hashtable is read-only.
IsFixedSizeboolIndicates whether the Hashtable has a fixed size.
KeysICollectionGets an ICollection containing the keys in the Hashtable.
ValuesICollectionGets an ICollection containing the values in the Hashtable.
ComparerIEqualityComparerGets the IEqualityComparer object that compares keys for the Hashtable.
SyncRootobjectGets an object that can be used to synchronize access.
IsSynchronizedboolIndicates whether access to the Hashtable is synchronized (thread safe).

Methods

SignatureDescription
void Add(object key, object? value)Adds an element with the specified key and value.
bool Contains(object key)Determines whether the Hashtable contains a specific key.
void Clear()Removes all elements from the Hashtable.
object? this[object key] { get; set; }Gets or sets the value associated with the specified key.
IDictionaryEnumerator GetEnumerator()Returns an enumerator that iterates through the Hashtable.
bool Remove(object key)Removes the element with the specified key.
void Clone()Creates a shallow copy of the Hashtable.
void GetObjectData(SerializationInfo info, StreamingContext context)Implements the ISerializable interface and populates a SerializationInfo with the data needed to serialize the target object.
void OnDeserialization(object? sender)Implements the IDeserializationCallback interface and raises the deserialization event when the entire object graph has been deserialized.
static bool Synchronized(Hashtable table)Returns a synchronized (thread-safe) wrapper for the Hashtable.

Examples

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        Hashtable ht = new Hashtable();

        // Adding key/value pairs
        ht.Add("Apple", 1);
        ht.Add("Banana", 2);
        ht["Cherry"] = 3;

        // Retrieve a value
        Console.WriteLine($"Apple = {ht["Apple"]}");

        // Iterate through the Hashtable
        foreach (DictionaryEntry entry in ht)
        {
            Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
        }

        // Check for a key
        if (ht.ContainsKey("Banana"))
            Console.WriteLine("Banana is present.");
    }
}