User Id :    Password :      New Member   Forgot Password  
Hashtable in CSharp
Description Hashtable is a collection that stores information using a mechanism called hashing. In hashing, we have key and value. Each value in the hashtable is determined by its key associated with it.   No. of Views     2900
  Rating     4
Author Md. Moinuddin   Posted On     29 May 2010
Tags ASP.NET,C#,Collection    

Sample Code   Download Code

Hashtable is a collection that stores information using a mechanism called hashing.

In hashing, we have key and value. Each value in the hashtable is determined by its key associated with it. The transformation of the key into its hash code is performed automatically—you never see the hash code, itself. The advantage of hashing is that it allows the execution time of lookup, retrieve, and set operations to remain constant, even for large sets. Hashtable implements the IDictionary, ICollection, IEnumerable, ISerializable, and ICloneable interfaces.

Hashtable defines several methods. Some of the commonly used methods are

ContainsKey( )
To determine if a Hashtable contains a key, call ContainsKey( ).

public virtual bool ContainsKey(object k)
Returns true if k is a key in the invoking Hashtable. Returns false otherwise.

To see if a specific value is present in the Hashtable, call ContainsValue()

public virtual bool ContainsValue(object v)
Returns true if v is a value in the invoking Hashtable. Returns false otherwise.

To add key/value in the hashtable,use Add() method.

Public void Add(string Key, object Value)

Hashtable adds several public properties. You can obtain a collection of a Hashtable’s keys or values by using the properties shown here:

public virtual ICollection Keys { get; }
public virtual ICollection Values { get; }

Hashtable does not maintain an ordered collection so Hashtable does not guarantee the order of its elements because the process of hashing does not usually lend itself to the creation of sorted tables.

Hashtable stores key/value pairs in the form of a DictionaryEntry structure, but most of the time you won’t be aware of it directly, because the properties and methods work with
keys and values individually. For example, when you add an element to a Hashtable, you call Add( ), which takes two arguments: the key and the value.

Here is an example that demonstrates Hashtable:

// Demonstrate Hashtable.

using System; 
using System.Collections; 

class HashtableTest 
public void ShowHashtable()  
// Create a hash table. 
Hashtable ht = new Hashtable(); 
// Add elements to the table 
ht.Add("house", "Dwelling"); 
ht.Add("car", "Means of transport");
ht.Add("book", "Collection of printed words"); 
ht.Add("apple", "Edible fruit"); 
// Can also add by using the indexer. 
ht["tractor"] = "Farm implement"; 
// Get a collection of the keys. 
ICollection c = ht.Keys; 
// Use the keys to obtain the values. 
foreach(string str in c) 
Response.WriteLine(str + ": " + ht[str]); 
The output from this program is shown here:

tractor: Farm implement apple:
Edible fruit house: Dwelling
car: Means of transport

As the output shows, the key/value pairs are not stored in sorted order. Notice how the contents of the hash table ht were obtained and displayed. First, a collection of the keys was retrieved by use of the Keys property. Each key was then used to index the ht, yielding the value associated with each key. Remember, the indexer defined by IDictionary and implemented by Hashtable uses a key as the index.

About Author

About Author Moin is a project manager in Esoft company. His love to help people by writing technical articles. Md. Moinuddin
No Photo
Country India
Company e.Soft Technologies
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Posted By Munim on 16 Oct 2010 at 02:02 PM
Why not use a generic dictionary? Hashtable has its own purpose. You should have defined it first. It would be much more easier to use generic dictionary as it typed, and suits good. Value in Hashtable are Object and thus you have to cast it all the time when you need to use it, that requires boxing/unboxing each time. Do you really know when to use Hashtable? Well, I am pointing to a 3 years old performance test between SortedList, SortedDictionary, Dictionary and Hashtable. Please consider looking at
Posted By Kiran on 03 Jun 2010 at 11:04 AM
Nice article moin!!!!! But can you please tell me the scenario when hash table can be used.
Write your comment here.
Verification Code