Represents a collection of key/value pairs that are organized by the hash code of the key. This class cannot be inherited.
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.
A public static (Shared in Visual Basic) member of this type is thread-safe.
Any instance member is not guaranteed to be thread-safe.
[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.
mscorlib.dll
Initializes a new instance of the Hashtable class that is empty, has the default initial capacity, and uses the case-sensitive, ordinal comparison for keys.
Initializes a new instance of the Hashtable class that is empty, has the specified initial capacity, and uses the case-sensitive, ordinal comparison for keys.
Initializes a new instance of the Hashtable class that is empty, has the default initial capacity, and uses the specified IEqualityComparer.
Gets the number of key/value pairs contained in the Hashtable.
Gets a value indicating whether the Hashtable has a fixed size.
Gets a value indicating whether the Hashtable is read-only.
Gets an ICollection containing the keys in the Hashtable.
Gets an ICollection containing the values in the Hashtable.
Adds an element with the specified key and value to the Hashtable.
Removes all elements from the Hashtable.
Creates a shallow copy of the Hashtable.
Determines whether the Hashtable contains the specified key.
Determines whether the Hashtable contains the specified key.
Determines whether the Hashtable contains the specified value.
Returns an enumerator that iterates through the Hashtable.
Removes the element with the specified key from the Hashtable.
Hashtable inherits from Object and implements IDictionary, ICollection, IEnumerable, and ICloneable.
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.
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}");
}
}