Hashtable in C# with example

Hashtable is another useful data structure in C# that allow us to store data using key value pairs. let's learn how to use Hashtable in C# with examples.

How to use Hashtable in C# Program?
using System.Collections;

Hashtable htObj = new Hashtable();

C# Hashtable is a collection object that comes under System.Collections namespace, Hashtable is similar to generic Dictionary collection object, with some difference like Hashtable allow us to store data of different data type, which is not supported in dictionary object.

Hashtable Methods
  • Add

    Adds an item with a key and value into the hashtable,
    below is an example of adding key value in hashtable in C#.

    Hashtable htObj = new Hashtable();
    htObj.Add(1, "This is string");
    
  • Contains

    Checks whether the hashtable contains a specific key

  • Remove

    Removes the item with the specified key from the hashtable

  • Clear

    Removes all the items from the hashtable.

  • ContainsKey

    Checks if hashtable contains a specific key

  • ContainsValue

    Checks if the hashtable contains a specific value.

Hashtable Properties
  • IsReadOnly

    Gets boolean value indicating whether the Hashtable is read-only.

  • Item

    Gets or sets the value associated with the specified key.

  • Keys

    Gets an ICollection of keys in the Hashtable.

  • Values

    Gets an ICollection of values in the Hashtable.

    Below is an example of how to get value from hashtable object in C#

    Hashtable htObj = new Hashtable();
    htObj.Add(1, "This is string");
    string strValue1 = (string)htObj[1];
    
  • Count

    Gets the total count of keys in the Hashtable.

How to Implement Hashtable in C#

Remember that hashtable works on key/value combination, key or value can be of any data type, look at the example below, this is how we can add key/value to Hashtable

Hashtable htObj = new Hashtable();
htObj.Add(1, "This is string");
htObj.Add(2, 2.2);
htObj.Add(3, true);
htObj.Add(4, DateTime.Now);
htObj.Add(5, null);
htObj.Add("Six", "see the key difference");
C# Hashtable Get value by Key

Now you see how to retrieve value from hashtable, As you have seen that hashtable store values with key, so you can retrieve value using same key, or you can hashtable get value by index.

string strValue1 = (string)htObj[1];
Console.WriteLine(strValue1);
                
DateTime? strValue4 = (DateTime?)htObj[4];
Console.WriteLine(strValue4);

As you have seen that a Hashtable can contains a key and a value of any data type. So while casting, values must be cast to an appropriate data type otherwise it will give compile-time error.

Notice in above example while retrieving data from hashtable, one converted to string and four converted to datetime

Loop through Hashtable c#

Now we have seen that in hashtable we can have different data type for key and values both field, but ideally we should have only same data type for key fields. Now let's try to loop through a hashtable c#, all the key values of above hashtable dynamically, htObj.Keys will get all the keys.

foreach (int key in htObj.Keys)
{
    Console.WriteLine("Key: " + key + " Value: " + htObj[key]);
}
c# hashtable example

See the error for last key, which is a different data type, when all keys are integer and the last (six) key is a string

So, remember to keep all the keys of same data type while creating a hashtable in C#

Or, instead of strongly type, we could have written var, like foreach (var key in htObj.Keys)

Difference between Hashtable and Dictionary

  • Hashtable is non generic and dictionary is generic
  • Hashtable can store any type of value, dictionary can store same type of value.

You should also look at others C# Data Structures Fundamental.

 
Hashtable in C# Tutorial
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
.Net C# Examples | Join .Net C# Course