HashTable Class in C#

In this article we will discuss about the HashTable Collection Class available in C#.
  • 2989

In this Collection Class it stores the item as key-value pairs. In it each item of the table is uniquely identify by the key and the key should be unique and value can be same or duplicate. The HashTable stores the key and value pair as the object type which is capable of storing any kind of value whether it is string, integer or long etc. The collection of the key and value pair can be retrieved from from HashTable using Keys and Pairs property. Through DictionaryEntry Class we can fetch key+value pair at a time.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace ConsoleApplication13

{

    class Program

    {

        static void Main(string[] args)

        {

            Hashtable ht = new Hashtable();

            ht.Add(1,"Honey");

            ht.Add(2, "Aman");

            ht.Add(3, "Rahul");

            ht.Add(4, "Swati");

            ht.Add(5, "Kirti");

            Console.WriteLine(ht[2].ToString());

            foreach (DictionaryEntry de in ht)

            {

                Console.WriteLine("Key is:"+de.Key.ToString()+"value is:"+de.Value.ToString());

            }

            Console.ReadLine();

 

        }

    }

}

Output:

image6.jpg

Further Readings
 
You may also want to read these related articles.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2019 DotNetHeaven. All rights reserved.