This example illustrates how to create and use a hash table. Each element of hash table is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

This example illustrates how to create and use a hash table. Each element of hash table is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

First, you will need to import the using System.Collections and System.IO namespace.

The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.
The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

The following example shows how to determine whether the Hashtable contains a specific element. We can search a hash table either by the keys, or by the associated values; But search by key will make an excellent search efficiency. We use the btnID_Click and btnName_click event to do the work. We then call the Hashtable.Contains Method to use the collection’s objects’ Equals and CompareTo methods on item to determine whether item exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection

The front end HashtableDemoCsharp.aspx page looks something like this:

The flow for the code behind page is as follows.

Download Source Files