C# Hashtable (With Examples)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

8/27/2021 C# Hashtable (With Examples)

C# - Hashtable

The  Hashtable   is a non-generic collection that stores key-value pairs, similar to


generic  Dictionary<TKey, TValue>  collection. It optimizes lookups by computing the hash
code of each key and stores it in a different bucket internally and then matches the hash
code of the specified key at the time of accessing values.

Hashtable Characteristics
Hashtable  stores key-value pairs.
Comes under  System.Collection  namespace.
Implements IDictionary interface.
Keys must be unique and cannot be null.
Values can be null or duplicate.
Values can be accessed by passing associated key in the indexer e.g.  myHashtable[key]
Elements are stored as DictionaryEntry objects.

Creating a Hashtable

The following example demonstrates creating a Hashtable and adding elements.

Example: Create and Add Elements

Hashtable numberNames = new Hashtable();

numberNames.Add(1,"One"); //adding a key/value using the Add() method

numberNames.Add(2,"Two");

numberNames.Add(3,"Three");

//The following throws run-time exception: key already added.

//numberNames.Add(3, "Three");

foreach(DictionaryEntry de in numberNames)

Console.WriteLine("Key: {0}, Value: {1}", de.Key, de.Value);

//creating a Hashtable using collection-initializer syntax

var cities = new Hashtable(){

{"UK", "London, Manchester, Birmingham"},

{"USA", "Chicago, New York, Washington"},

{"India", "Mumbai, New Delhi, Pune"}

};

foreach(DictionaryEntry de in cities)

Console.WriteLine("Key: {0}, Value: {1}", de.Key, de.Value);

Try it

The  Hashtable  collection can include all the elements of Dictionary, as shown below.

https://www.tutorialsteacher.com/csharp/csharp-hashtable 1/4
8/27/2021 C# Hashtable (With Examples)

Example: Add Dictionary in Hashtable

Dictionary<int, string> dict = new Dictionary<int, string>();

dict.Add(1, "one");
dict.Add(2, "two");
dict.Add(3, "three");

Hashtable ht = new Hashtable(dict);

Try it

Update Hashtable

You can retrieve the value of an existing key from the  Hashtable  by passing a key in indexer.
The  Hashtable  is a non-generic collection, so you must type cast values while retrieving it.

Example: Update Hashtable

//creating a Hashtable using collection-initializer syntax

var cities = new Hashtable(){

{"UK", "London, Manchester, Birmingham"},

{"USA", "Chicago, New York, Washington"},

{"India", "Mumbai, New Delhi, Pune"}

};

string citiesOfUK = (string) cities["UK"]; //cast to string

string citiesOfUSA = (string) cities["USA"]; //cast to string

Console.WriteLine(citiesOfUK);

Console.WriteLine(citiesOfUSA);

cities["UK"] = "Liverpool, Bristol"; // update value of UK key

cities["USA"] = "Los Angeles, Boston"; // update value of USA key

if(!cities.ContainsKey("France")){

cities["France"] = "Paris";

Try it

ADVERTISEMENT

https://www.tutorialsteacher.com/csharp/csharp-hashtable 2/4
8/27/2021 C# Hashtable (With Examples)

Remove Elements in Hashtable

The  Remove()  method removes the key-value that match with the specified in the  Hashtable .
It throws the  KeyNotfoundException   if the specified key not found in the Hashtable, so check
for an existing key using the  ContainsKey()  method before removing.

Use the  Clear()  method to remove all the elements in one shot.

Example: Remove Elements from Hashtable

var cities = new Hashtable(){

{"UK", "London, Manchester, Birmingham"},

{"USA", "Chicago, New York, Washington"},

{"India", "Mumbai, New Delhi, Pune"}

};

cities.Remove("UK"); // removes UK

//cities.Remove("France"); //throws run-time exception: KeyNotFoundException

if(cities.ContainsKey("France")){ // check key before removing it

cities.Remove("France");

cities.Clear(); //removes all elements

Try it

Hashtable Class Hierarchy

The following diagram illustrates the Hashtable class hierarchy.

https://www.tutorialsteacher.com/csharp/csharp-hashtable 3/4
8/27/2021 C# Hashtable (With Examples)

https://www.tutorialsteacher.com/csharp/csharp-hashtable 4/4

You might also like