SIGN UP MEMBER LOGIN:    
ARTICLE

Hashtable in C#

Posted by Mahesh Chand Articles | How do I November 14, 2005
Hashtable is useful when you need to store data in a key and value pair. This article shows how to use hashtable in C#.
Reader Level:

This quick how do I shows how to use a Hashtable in C#.

Creating a Hashtable

The Hashtable class in C# represents a hashtable. The following code creates a Hashtable:

private Hashtable hshTable = new Hashtable();

Adding Items to a Hashtable

Add method of Hashtable is used to add items to the hashtable. The method has index and value parameters. The following code adds three items to hashtable.

hshTable .Add("Author1",  "Mahesh Chand");
hshTable .Add("Author2",  "James Brand");
hshTable .Add("Author3",  "Mike Gold");

Retrieving an Item Value from Hashtable

The following code returns the value of "Author1" key:

string name = hshTable["Author1"].ToString();

Removing Items from a Hashtable

The Remove method removes an item from a Hashtable. The following code removes item with index "Author1" from the hashtable:

hshTable.Remove("Author1");

Looking through all Items of a Hashtable

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

// Loop through all items of a Hashtable
IDictionaryEnumerator en = hshTable.GetEnumerator();
while (en.MoveNext())
{
      string str = en.Value.ToString();
}

Login to add your contents and source code to this article
share this article :
post comment
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace indexingform
{
    public partial class Form1 : Form
    {

     
        public Form1()
        {
            InitializeComponent();
        }

        Hashtable hs = new Hashtable();
        indexing.student obj;

        private void Form1_Load(object sender, EventArgs e)
        {
            obj = new indexing.student();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            obj.rollno = Int32.Parse(textBox1.Text);
            obj.stuname = textBox2.Text;
            obj.age = Int32.Parse(textBox3.Text);
            hs.Add(obj.rollno,obj);
           


        }

        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show("total record=" + hs.Count.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (hs.Count > 0)
            {
               obj.rollno= 1;
                indexing.student oo = (indexing.student)hs[obj.rollno];

                //textBox1.Text = oo.rollno.ToString();
                textBox2.Text = oo.stuname;
                textBox3.Text = oo.age.ToString();
            }
            else
            {
                MessageBox.Show("record not found");
            }
                   
        }
    }
}
i have a problem in the bold and under line code  its a nevigation botton codeing searching for the first enterd vale

Posted by praveen gaur Jul 07, 2010

Hi mahesh, thank you for the article.
I was wondering if i can involve multiple values in the hashtable item.

Example, suppose i have an item as:
hshTable .Add("imageModify",  "C:\execute.cs","inputFileLocation","outputFileLocation");

When i enter imageModify and Invoke it then the execute.cs will need to recognise imageModify and the fileLocations should be sent so that exexute.cs recognises which is the image to be modified and where it needs to be saved after executing the function in the cs file. Can i do this using Hashtables or is there some  other method for this???

Posted by scorpion king Feb 24, 2010

good

Posted by kashif abbasi Jun 12, 2009

I found this article is very useful and made my job simple and easy.

Thank you

Murali

Posted by muralidharan vedachalam Dec 17, 2008

can indexing be done in hashtable?

Posted by vidushi joshi Oct 23, 2008
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor