A hashtable is an associative
array (dictionary) that
contains key-value pairs. Each value is identified and retrieved by a
specific key that is transformed into an integer value called a
hashcode.
A hashtable is an efficient way to store and retrieve values in
memory. It uses a fast algorithm to convert a hashcode into a hash
key. This hash key is used internally to determine which
"bucket" a hashtable entry belongs
to. Although the algorithm selects a bucket quickly, each bucket may
contain more than one value. In this case, a linear search locates
the desired value based on its hashcode. However, the fast bucket
search offers such an advantage that a subsequent linear search has a
negligible impact on the overall performance of the hashtable.
Initially, a 1-to-1 ratio of buckets to values applies (called the
load factor). However, as more items are added
to the hashtable, the load factor is changed, and each bucket ends up
holding more elements. Greater load factors reduce the amount of
memory required to store the hashtable, but increase lookup time.
The first argument to the Hashtable constructor
gives a value for its initial size or provides an existing
IDictionary whose values will fill the
Hashtable. A Hashtable
automatically increases its size when all buckets are full. The
loadFactor argument is optionally used to specify
the load factor; the default is 1.0. You can also provide references
to IHashCodeProvider and
IComparer instances in the constructor to provide
custom hashcode and key-sorting functionality.
Keys of varying types can be used as in a regular dictionary
collection. A hashing algorithm is used to convert the key into the
hashcode. This is accomplished by the GetHashCode(
) method of each key object, which is a virtual method
provided by Object. GetHashCode(
) can be overridden to use a custom algorithm instead of
the default hashing algorithm provided by the CLR. (See
CaseInsensitiveHashCodeProvider.)
The Keys and Values properties
retrieve ICollection objects containing the keys
and values, respectively, of the Hashtable.
The Hashtable indexer allows you to get or
retrieve a value by specific key. If a key already exists, its value
is overwritten. The Add( ) method can also add a
new key and value to a Hashtable, but throws an
exception if the key already exists.
public class Hashtable : IDictionary, ICollection, IEnumerable, System.Runtime.Serialization.ISerializable,
System.Runtime.Serialization.IDeserializationCallback, ICloneable {
// Public Constructors
public Hashtable( );
public Hashtable(IDictionary d);
public Hashtable(IDictionary d, IHashCodeProvider hcp, IComparer comparer);
public Hashtable(IDictionary d, float loadFactor);
public Hashtable(IDictionary d, float loadFactor, IHashCodeProvider hcp, IComparer comparer);
public Hashtable(IHashCodeProvider hcp, IComparer comparer);
public Hashtable(int capacity);
public Hashtable(int capacity, IHashCodeProvider hcp, IComparer comparer);
public Hashtable(int capacity, float loadFactor);
public Hashtable(int capacity, float loadFactor, IHashCodeProvider hcp, IComparer comparer);
// Protected Constructors
protected Hashtable(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context);
// Public Instance Properties
public virtual int Count{get; }
// implements ICollection
public virtual bool IsFixedSize{get; }
// implements IDictionary
public virtual bool IsReadOnly{get; }
// implements IDictionary
public virtual bool IsSynchronized{get; }
// implements ICollection
public virtual ICollection Keys{get; }
// implements IDictionary
public virtual object SyncRoot{get; }
// implements ICollection
public virtual object this[object key]{set; get; }
// implements IDictionary
public virtual ICollection Values{get; }
// implements IDictionary
// Protected Instance Properties
protected IComparer comparer{set; get; }
protected IHashCodeProvider hcp{set; get; }
// Public Static Methods
public static Hashtable Synchronized(Hashtable table);
// Public Instance Methods
public virtual void Add(object key, object value);
// implements IDictionary
public virtual void Clear( );
// implements IDictionary
public virtual object Clone( );
// implements ICloneable
public virtual bool Contains(object key);
// implements IDictionary
public virtual bool ContainsKey(object key);
public virtual bool ContainsValue(object value);
public virtual void CopyTo(Array array, int arrayIndex);
// implements ICollection
public virtual IDictionaryEnumerator GetEnumerator( );
// implements IDictionary
public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
// implements ISerializable
public virtual void OnDeserialization(object sender)
// implements System.Runtime.Serialization.IDeserializationCallback
public virtual void Remove(object key);
// implements IDictionary
// Protected Instance Methods
protected virtual int GetHash(object key);
protected virtual bool KeyEquals(object item, object key);
}