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();
- }

ShehzadPosted Apr 18, 2016, 7:46 PM
Thanks Sir...for Sharing and Effort
Azharunissa KhanPosted Jan 8, 2014, 11:28 PM
sir, I have to embed a path in the "value " attribute of the hash table.I mean to say ,as in the hash table we have key and value pairs... i want to place text values in the "KEY" field and path of a file in "VALUE" field.How can i do that? could you please suggest me or provide me a relevant code?
Former memberPosted Oct 27, 2012, 6:47 AM
Hi this is really nice article . please check this also. http://dotnetpools.com/Article/ArticleDetiail/?articleId=53&title=Code%20To%20Use%20HashTable%20in%20C#.net
praveen gaurPosted Jul 7, 2010, 12:47 PM
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
scorpion kingPosted Feb 24, 2010, 11:37 AM
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???
kashif abbasieditedPosted Jun 12, 2009, 6:57 AMEdited May 13, 2012, 10:44 PM
good
muralidharan vedachalameditedPosted Dec 17, 2008, 5:48 PMEdited Dec 17, 2008, 5:48 PM
I found this article is very useful and made my job simple and easy. Thank you Murali
vidushi joshiPosted Oct 23, 2008, 1:55 AM
can indexing be done in hashtable?