Home > DeveloperSection > Blogs > C# Hashtable

C# Hashtable


.NET C#  .NET 
Ratings:
2 Comment(s)
 1363  View(s)
Rate this:

C# Hashtable

In this blog I’m explaining about Hashtable class in C#.

Hashtable:

The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.

A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

 

Creating Hashtable:

The hashtable class in c# represent a Hashtable.

Hashtable hashtable=new Hashtable();

 

Adding items in Hashtable with method:

Add method of Hashtable is used to add items into the Hashtable. The method has index and value parameters.

hashtable.Add(“UP 70”,”Allahabad”);

hashtable.Add(“UP 72”,”PratapGarh”);

hashtable.Add(“UP 66”,”Bhadohi”);

 

Adding items in Hashtable directly:

First you can create object of Hashtable, Hashtable has no values we directly assign with the indexer which uses the square brackets.

Hashtable hashtable=new Hashtable();

Hashtable [0]=”Kamlakar”;

Hashtable [1]=”Rohit”;

Hashtable [4]=”Pawan”;

 

Retrieving an item value from Hashtable:

the fallowing code returns the value of “UP 70”key.

string cityname=hashtable[“UP 70”].ToString();

 

Removing items from Hashtable:

Removing items from Hashtable for achive this task use Remove () method of Hashtable class the fallowing code remove items with particular index from hashtable.

 

Hashtable.Remove(“UP 70”);

 

Looking all items of Hashtable:

The fallowing code loops through all items of a hashtable and reads the values.

IDictionaryEnumerator en = hshTable.GetEnumerator();

While(en.MoveNext())

{

            string st=en.Value.ToString();

}          

 

using System;

using System.Collections;

 

namespace ExHashtable

{

    class Program

    {

        static void Main(string[] args)

        {

            Hashtable hashtable = new Hashtable();

            hashtable.Add("UP 70","Allahabad");

            hashtable.Add("UP 72","PratapGarh");

            hashtable.Add("UP 66","Bhadohi");

            if (hashtable.ContainsValue("Banaras"))

            {

                Console.WriteLine("This student name is already in the list");

            }

            else

            {

                hashtable.Add("UP 65", "Banaras");

            }

            ICollection key = hashtable.Keys;

 

            foreach (string k in key)

            {

                Console.WriteLine(k + ": " + hashtable[k]);

            }

            Console.ReadKey();

        }

    }

}

 

Output

 

Example retrieving item value from Hashtable:

using System;

using System.Collections;

 

namespace ExHashtable

{

    class Program

    {

        static void Main(string[] args)

        {

            Hashtable hashtable = new Hashtable();

            hashtable.Add("UP 70","Allahabad");

            hashtable.Add("UP 72","PratapGarh");

            hashtable.Add("UP 66","Bhadohi");

            string cityname=hashtable["UP 70"].ToString();

            Console.WriteLine(cityname);

        }

    }

}

 

Output

 

Example of removing items from Hashtable:

using System;

using System.Collections;

 

namespace ExHashtable

{

    class Program

    {

        static void Main(string[] args)

        {

            Hashtable hashtable = new Hashtable();

            hashtable.Add("UP 70","Allahabad");

            hashtable.Add("UP 72","PratapGarh");

            hashtable.Add("UP 66","Bhadohi");

            hashtable.Remove("UP 66");

            ICollection key = hashtable.Keys;

            foreach (string k in key)

            {

                Console.WriteLine(k + ": " + hashtable[k]);

            }

            Console.ReadKey();

        }

    }

}

 

Output

 in my next post i'll explain about Bootstrap ContextMenu


what is the dffrence between HashTable and Dictionary

By ram ratan on   2 years ago
hi guys
this stuff is nice 
but how we use Dictionary Instead of HashTable
i got big confusion between of them
please any body tel me
i got something on that portal where i doing training they told me both are working as similar
but not clearly understandable whats the difference between them
please any body tell me write answer
thanks

Hi ram ratan

By Kamlakar Singh on   2 years ago
If you meant "why do we use the Dictionary class instead of the Hashtable class?", then it's an easy answer: Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out.

Don't want to miss updates? Please click the below button!

Follow MindStick