Mono Class Library: System.Collections NamespaceSystem.Collections.Hashtable Class |
See Also: Hashtable Members
|
This class is safe for multiple readers and a single writer.
A Hashtable represents a dictionary with a constant lookup time that contains entries of associated keys and values. The type of each entry in a Hashtable is DictionaryEntry. A statement that exposes each element in the collection is required to iterate over this type.
Note: See example.Objects used as keys in a Hashtable are required to either implement both object.GetHashCode and object.Equals(object) or neither. Furthermore, for a particular key, these methods are required to produce the same results when called with the same parameters while that key exists in a particular Hashtable . Keys cannot be mutated while they are used in the table.
Every key in a Hashtable is required to be unique compared to every other key in the table. An object that implements IComparer can determine whether two keys are unequal. The default comparer for a key is the key's implementation of object.Equals(object).
Each value in a Hashtable is required to provide its own hash function, which can be accessed by calling Hashtable.GetHash(object). Alternatively, if an object that implements IHashCodeProvider is passed to a Hashtable constructor, the custom hash function provided by that object is used for every value in the table.
Note:[Edit]The default capacity (i.e. the default number of entries that can be contained) of a Hashtable is zero.
When an entry is added to the Hashtable, the entry is placed into a bucket based on the hash code obtained from the IHashCodeProvider implementation of the table, or the object.GetHashCode if no specific IHashCodeProvider was provided. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, substantially reducing the number of key comparisons required to find an entry.
As entries are added to a Hashtable, and the maximum capacity of the table is reached, the number of buckets in the table is automatically increased to the smallest prime number that is larger than twice the current number of buckets.
A Hashtable can safely support one writer and multiple readers concurrently. To support multiple writers, all operations are required to be done through the wrapper returned by the Hashtable.Synchronized(Hashtable) method.
The following example shows how to iterate over the elements of a Hashtable.
[C#]
foreach (DictionaryEntry myEntry in myHashtable)
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0