Hashtable Class in C#
Hashtable is a collection of key/value pairs that are organized
based on the hash code of the key. When elements are added to a hash table, a
hash code is generated automatically. This code is hidden from the developer.
It uses key to access elements in collection. Hashtable optimizes lookups with the help of keys.
Now, we are
creating a Hashtable ‘ht’ using its
default constructor. The syntax are follows:
Hashtable ht=new Hashtable ();
// adding a key and value in Hashtable
ht.Add (“1”, “One”);// Here ‘1’ is key and ‘One’ is value.
Now, we will
display the element by using foreach
loop. For retrieving elements through the Hashtable by using the DictionaryEntry type in a foreach loop.
foreach (DictionaryEntry e in ht)
Methods of Hashtable:
Some important methods of Hashtable
Add: - Adds an elements with the specified key/value in the Hashtable.
Clear: - Removes all elements in the Hashtable.
3. Contains Key: - The ContainsKey() method returns
true if key is found in hashtable otherwise,
it returns false. If we want to find value are in the hashtable
Contains: We use Contains () method. When, we want key/value is found or not in the Hashtable?
For checking keys:
For checking Value:
int value = (int) ht[“One”]; // we need to casting as data type
Some of the important properties of a Hashtable is as follows:-
Keys: Gets an ICollection containing the keys in the Hashtable.
Values: Gets an ICollection containing the Values in the Hashtable.
Comparer: Gets or sets the IComparer to use for
Item: Gets or sets the values associated
with the specified keys.
IsReadOnly: Gets a value indicating whether the Hashtable is readonly.
We can use
the ‘as’ operator to attempt to cast
an object to a specific reference type .it returns true or false and you also
reduce casting by using the ‘is’ operator.
Hashtable ht= new Hashtable();
string v = ht as string;
Console.WriteLine(“value is: ”+v);
StreamReader r= ht as StreamReader
If (v2 is string)
Console.WriteLine(“Value is a string type:”+v2);
Value is Test
Value is a string type Test
If you want to store keys in
storing keys in arraylist you can use Keys
property of Hashtable. For example,
ArrayList al= new ArrayList(ht.Keys)
// For retrieving elements in the arraylist
foreach(int key in al)r
Hashtable allows the
execution time for lookup, retrieve and set operation to remain nearly constant,
even for large sets.
In large data sets, Hashtable is ability
to locate the item quickly.
No need to scan through the entire data sets to find items.
For example, we create an instance of Hashtable and adding four elements in it. Then we are printing
all elements in the Hashtable and keys
given to the arraylist for containing all keys of Hashtable
and finally print it.
static void Main(string args)
Hashtable ht = new Hashtable();
ICollection key = ht.Keys;
Console.WriteLine("Retrieving all elements: ");
foreach(var k in key)
Console.WriteLine(k + ":" + ht[k]);
ArrayList al = new ArrayList(key);
Console.WriteLine("Retrieving all keys in the arraylist");
foreach (var n in al)