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.

Modifying Hashtable Contents

Adding an Item

You can add items to a Hashtable in the same manner as for all collections that implement IDictionary. However there are two important restrictions. Firstly, the key of an item to be added must not be null. The second is that the key must be unique. These restrictions to not apply to the value part of a Hashtable entry.

Hashtable myData = new Hashtable();
myData.Add("key1","value1");
myData.Add("key1","value2");                // Duplicate: ArgumentException
myData.Add(null,"value2");                  // Null Key: ArgumentNullException

Updating Using the Key

Hashtable contents can be modified directly using the key as if it were an array's index number. The key is appended to the collection's name between square brackets. If you assign to a key that exists, the value of the corresponding item is updated. If the key does not exist, the key and the value are added to the Hashtable as a new item.

Hashtable fruit = new Hashtable();
fruit["apple"] = "Granny Smith";            // New item created
fruit["apple"] = "Golden Delicious";        // Item updated
fruit["banana"] = "Cavendish";
fruit["cherry"] = "Yamagata";

Reading Hashtable Information

One of the key benefits of the Hashtable collection is the facility to directly read an entry using its key as a reference. This is the simplest way to retrieve an item. As with arrays and index-based collections, the key is appended to the collection name in a pair of square brackets.

NB: The following examples use strings for keys and values to demonstrate the use of Hashtables. Remember that when developing your own programs, both the key and value items can be any object type.

Hashtable fruit = new Hashtable();
fruit.Add("apple","Granny Smith");
fruit.Add("banana","Cavendish");
fruit.Add("cherry","Yamagata");
fruit.Add("orange","Seville");

string banana = fruit["banana"].ToString();
Console.WriteLine(banana);                  // Outputs "Cavendish"

Iterating Using DictionaryEntry Objects

When you add an item to a dictionary, the key and the value elements are combined into a single object of the type DictionaryEntry. Using the foreach statement, it is possible to loop through each item and extract its key and value from the DictionaryEntry object. The following example outputs the details of each key / value pair to the console. Note that the order of the items in the collection is not the same as the order in which they were added.

Hashtable fruit = new Hashtable();
fruit.Add("apple","Granny Smith");
fruit.Add("banana","Cavendish");
fruit.Add("cherry","Yamagata");
fruit.Add("orange","Seville");

foreach(DictionaryEntry de in fruit)
{
    Console.WriteLine("{0} \t: {1}", de.Key, de.Value);
}

/* OUTPUT

cherry  : Yamagata
apple   : Granny Smith
banana  : Cavendish
orange  : Seville

*/

Values

In many cases you will not want to iterate through the list of DictionaryEntry objects as you will only want to interrogate the values. The Values property returns an ICollection object that contains only the values from the Hashtable. This can be looped through directly.

Hashtable fruit = new Hashtable();
fruit.Add("apple","Granny Smith");
fruit.Add("banana","Cavendish");
fruit.Add("cherry","Yamagata");
fruit.Add("orange","Seville");

foreach(string value in fruit.Values)
{
    Console.WriteLine(value);
}

/* OUTPUT

Yamagata
Granny Smith
Cavendish
Seville

*/

Keys

The Keys property is similar to Values. It returns an ICollection containing all of the keys in a Hashtable.

Hashtable fruit = new Hashtable();
fruit.Add("apple","Granny Smith");
fruit.Add("banana","Cavendish");
fruit.Add("cherry","Yamagata");
fruit.Add("orange","Seville");

foreach(string value in fruit.Keys)
{
    Console.WriteLine(value);
}

/* OUTPUT

cherry
apple
banana
orange

*/
20 May 2007