Hashtable class

Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents

What is the Hashtable?

The Hashtable class represents a collection of key/value pairs that are organized based on the hash code of the key. Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference , but a value can be.

Things To Keep In Mind When Creating The Hashtable

The objects used as keys by a Hashtable are required to override the Object.GetHashCode method (or the IHashCodeProvider interface) and the Object.Equals method (or the IComparer interface). The implementation of both methods and interfaces must handle case sensitivity the same way; otherwise, the Hashtable might behave incorrectly. For example, when creating a Hashtable, you must use the CaseInsensitiveHashCodeProvider class (or any case-insensitive IHashCodeProvider implementation) with the CaseInsensitiveComparer class (or any case-insensitive IComparer implementation). Furthermore, these methods must produce the same results when called with the same parameters while the key exists in the Hashtable.

An alternative is to use a Hashtable constructor with an IEqualityComparer parameter. If key equality were simply reference equality, the inherited implementation of Object.GetHashCode and Object.Equals would suffice. Key objects must be immutable as long as they are used as keys in the Hashtable.


How The Hashtable Works

When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element. The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size.

A different load factor can also be specified when the Hashtable is created. As elements are added to a Hashtable, the actual load factor of the Hashtable increases. When the actual load factor reaches the specified load factor, the number of buckets in the Hashtable is automatically increased to the smallest prime number that is larger than twice the current number of Hashtable buckets.

Each key object in the Hashtable must provide its own hash function, which can be accessed by calling GetHash. However, any object implementing IHashCodeProvider can be passed to a Hashtable constructor, and that hash function is used for all objects in the table. The capacity of a Hashtable is the number of elements the Hashtable can hold. As elements are added to a Hashtable, the capacity is automatically increased as required through reallocation. The foreach statement of the C# language requires the type of each element in the collection.

Since each element of the Hashtable is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection. Because serializing and deserializing an enumerator for a Hashtable can cause the elements to become reordered, it is not possible to continue enumeration without calling the Reset method. Because keys can be inherited and their behavior changed, their absolute uniqueness cannot be guaranteed by comparisons using the Equals method.


Code Examples

Adding Elements To The Table

// Add some elements to the hash table. There are no duplicate keys, but some of the values are duplicates.
exampleTable.Add( "txt", "notepad.exe" );
exampleTable.Add( "bmp", "paint.exe" );
exampleTable.Add( "dib", "paint.exe" );
exampleTable.Add( "rtf", "wordpad.exe" );

Using The Default Item Property To Change Its Corresponding Key

// The default Item property can be used to change the value associated with a key.
exampleTable[ "rtf" ] = "winword.exe";
 
// If a key does not exist, setting the default Item property
// for that key adds a new key/value pair.
exampleTable[ "doc" ] = "winword.exe";
 
//ContainsKey can be used to test keys before inserting them.
if( !exampleTable.ContainsKey( "ht" ) )
{
  exampleTable.Add( "ht", "hypertrm.exe" );
}

Using The Remove Method To Delete A Value Or Key From The Table

// Use the Remove method to remove a key/value pair.
exampleTable.Remove( "doc" );

MSDN References


prevpp.png  nextpp.png
C# Online.NET