BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The Hashtable Collection

The thirty-eighth part of the C# Fundamentals tutorial describes the use of the Hashtable class. This provides general-purpose dictionary collections allowing the items in a collection to be addressed not by an index number, but by an object-based key.

What is a Hash Table?

In computing, a hash table is a data structure that stores a set of values that can each be identified by a unique key. The key can be used as a lookup to find any item in the set. However, rather than storing the unique key value directly, it is hashed. Hashing converts the key into an index number and the value data linked to the key is placed in a storage location defined by that index.

The main advantage of the hash table structure is the ability to rapidly locate items, even in huge sets of data. If the key for a desired data value is known, the key can be hashed and the value's location found directly. There is no requirement to scan through the entire data set to find an item or to sort the set in order to perform a binary search.

The Hashtable Collection

The .NET framework provides the Hashtable class, which has all of the functionality required to implement a hash table with no additional development. The Hashtable is a general-purpose dictionary collection. Each item within the collection is a DictionaryEntry object with two properties: a key object and a value object. These are known as key / value pairs.

When items are added to a Hashtable, a hash code is generated automatically. This code is hidden from the developer. All access to the table's values is achieved using the key object for identification. As the items in the collection are sorted according to the hidden hash code, the items should be considered to be randomly ordered.

Implemented Collection Interfaces

The Hashtable collection implements the IDictionary and ICollection interfaces. This behaviour is described in the earlier Collection Interfaces article. The rest of this article describes the additional functionality of the Hashtable class.

Declaring a Hashtable

Constructors

In the .NET framework version 1,1, the Hashtable class includes ten constructors. Later releases of the framework include more. In this beginner's article, we will consider four constructors. The simplest requires no parameters. The following code creates a new, empty hash table. The Hashtable class is found in the System.Collections namespace so to execute the examples, add using System.Collections; to the source code.

Hashtable myData = new Hashtable();

New Hashtables have a limited capacity. When the limit is reached, the capacity is automatically increased to allow further storage. The storage requirements of a hash table structure are less predictable than those of other collections as the exact usage is determined by the hash values generated from item keys. To allow for this, when the size of a Hashtable is increased, the new capacity is set to the smallest prime number that is at least double the existing capacity. For example, if the initial capacity is 5, this is doubled to 10 and the next prime number, 11, is used as the new capacity.

The nature of a Hashtable dictionary means that the capacity is always considered to be approximate. It is possible to set the initial approximate capacity by passing it as an integer parameter to the constructor. This is useful when the maximum size of the collection is known as it removes the need for the Hashtable to be resized and improves performance.

Hashtable myData = new Hashtable(100);

All hash tables have a load factor. This is the ratio between the number of items in the collection and the number of buckets, or storage spaces available. The load factor for a Hashtable dictionary defaults to 1.0 to give a good balance between performance and memory requirements. The value may be altered in the constructor by specifying an initial capacity as an integer and a second, decimal parameter for the load factor. Valid values for the load factor are between 0.1 and 1.0. A value of 0.1 indicates that only one tenth of the storage space in the collection will be used before the Hashtable capacity is increased. This would give a high performing but memory-hungry dictionary.

Hashtable myData = new Hashtable(100, 0.1f);

The final constructor to be investigated allows a new Hashtable to be created and be fully populated using the contents of another collection that supports the IDictionary interface. Every item in the provided dictionary is copied into the new collection.

Hashtable myData = new Hashtable();
myData.Add("key1","value1");
Hashtable myCopy = new Hashtable(myData);   // Copies myData into myCopy
20 May 2007