July 15, 2024

HashTable in C#

Hashtable is like a container used to store data in key/value pairs. The keys are used as like index and helps to find out the values quickly. It is a .NET Framework component. This article describes basic overview of HashTable or how to use HashTable in C#. Summary of the article:

  • What is HashTable?
  • Example of HashTable
  • When to use a HashTable?
  • HashTable vs. Dictionary

What is HashTable?
A hashtable or a hash map is a data structure that is used to implement an associative array. It is user-defined reference types. It associates keys with values. The primary operation in hash table is look like: given a key (e.g. a student’s name), find the corresponding value (e.g. that student’s ID number). It uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found. It is an older .NET Framework. It is slower than the generic Dictionary type.

Example of HashTable
Example of HashTable in C#:

Hashtable hashtable = new Hashtable();
hashtable.Add(100, "Dhaka");
hashtable.Add(200, "London");

// Display the keys.
foreach (int key in hashtable.Keys)

// Display the values.
foreach (string value in hashtable.Values)

If we run the above C# code in a Console Application we will get the following output:
//For first loop

//For second loop

When to use a HashTable?

  • When we need to search items quickly by key
  • We can use IList or IEnumerable etc for a matching key but that will take O(n) time rather than O(1) for Hashtable or Dictionary

Hashtable vs. Dictionary
The Differences between Hashtable and Dictionary:


  1. It returns null if we try to find a key which does not exist
  2. It is slower than dictionary because it requires boxing and unboxing
  3. All the members in a Hashtable are thread safe
  4. It is not a generic type that means we can’t use it with any data type


  1. It returns error if we try to find a key which does not exist
  2. It is faster than a Hashtable because there is no boxing and unboxing
  3. Only public static members are thread safe
  4. It is a generic type that means we can use it with any data type

For some case we can use hashtable instead of database. Which can save time and cost. C# hashtable can play an important role in our programming.

Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

4 thoughts on “HashTable in C#

  1. I have noticed you don’t monetize your website, don’t waste your traffic, you can earn additional bucks every month because you’ve got hi quality content.
    If you want to know how to make extra $$$, search for: Boorfe’s tips best adsense alternative

  2. The brand new idea is here). I have read the post with great satisfaction and even could know something brand new I
    will use for my further requirements. The article is
    clear and bright, with no further useless facts or else, it reminded me https://missionwacolegalservices.org/legalworkshops/. The speech is both
    brilliant and vivid, so the more I read, the more I really do enjoy it!
    Anyway, the information is quite cutting edge,
    so just like it.

Leave a Reply

Your email address will not be published. Required fields are marked *