What are the uses of HashTable in C# Explain with example?

Asked 27-Aug-2021
Viewed 459 times

1 Answer


0

Hashtable is a non-generic collection class, which stores data in the key, value pair of different datatype. hashtable is the same as the dictionary object, but the difference between hashtable and dictionary is, a hashtable is stores different datatype and dictionaries are stored the same type of data. Hashtable will provide lower performance because it supports the heterogeneous datatype.

Example 
using System;

using System.Collections;
public class Program
{
 public static void Main()
 {
  Hashtable htbl = new Hashtable();
  htbl.Add(5,'Five');
  htbl.Add(1,'One');
  htbl.Add(2,'Two');
  htbl.Add(3,'Three');
  htbl.Add(4,'Four');
  foreach(DictionaryEntry item in htbl)
   Console.WriteLine('Key : ' + item.Key + ' Value : '+item.Value);
 }
}

Output

Key : 5 Value : Five

Key : 4 Value : Four
Key : 3 Value : Three
Key : 2 Value : Two
Key : 1 Value : One