Free Web Development Online Tutorials, Learn to Code
Hashtable in C#, Learn C# programming Language, Free C# Tutorial for Beginners

Hashtable in C# with example

What is Hashtable? learn how to use Hashtable in C# with examples.

What is Hashtable in C#?

C# Hashtable is a collection object that comes under System.Collections namespace, Hashtable is similar to generic Dictionary collection

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];
DateTime? strValue4 = (DateTime?)htObj[4];

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#

free C# tutorial online

Hashtable in C# Tutorial
Other Online Tutorial

C# .net Interview Questions Answers

.Net C# Examples | Join .Net C# Course