Hashtable in CSharpHashtable 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.
ContainsValue()
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.
Add()
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.