blog

Home / DeveloperSection / Blogs / C# Hashtable

C# Hashtable

Anonymous User3502 22-Sep-2014

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 following code returns the value of “UP 70”key.


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

 

Removing items from Hashtable:

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

 

Hashtable.Remove(“UP 70”);

 

Looking all items of Hashtable:

The following 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
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Hashtable hashtable = newHashtable();
            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

C# Hashtable

 

Example retrieving item value from Hashtable:

using System;
using System.Collections;
 
namespace ExHashtable
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Hashtable hashtable = newHashtable();
            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

C# Hashtable

 

Example of removing items from Hashtable:
using System;
using System.Collections;
 
namespace ExHashtable
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Hashtable hashtable = newHashtable();
            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

C# Hashtable

 in my next post i'll explain aboutBootstrap ContextMenu


Updated 22-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By