Collections are used to store generic data types. Some of the collection classes available in .NET Framework includes ArrayList, HashTable, Stack, and Queue.
In this article, I am going to explain the Hashtable class and how to use it. The Hashtable object contains items in key/value pairs. The keys are used as indexes. We can search value by using their corresponding key. Items are added to the Hashtable with the Add() method. The data type of Hashtable is object and the default size of a Hashtable is 16.
In this article, I am using a program to define and use a Hashtable object.
First of all, I created a class library and in this class library have a class that has two properties named as Roll and Name. I am saving these roll number and name in this class. Now I will use to store these in a Hashtable.
My class name is called HashTableClass, that is listed as following:
using System;
using System.Collections.Generic;
using System.Text;
namespace HashTableClass
{
public class Class1
{
int rollno;
string name;
public int Roll
{
get { return rollno; }
set { rollno = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
}
Now I create an application that will use my class library and add my DLL reference to my application.

Figure 1.
Add this namespace to use Hashtable.
using System.Collections;
The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.
Now here is my application class.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace AHashTablePresentation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int b = 1, key,j=0;
Hashtable ht = new Hashtable();
private void Button_Click(object sender, EventArgs e)
{
}
//To Search a perticular Record
private void button6_Click(object sender, EventArgs e)
{
j = Int32.Parse(textBox3.Text);
HashTableClass.Class1 obj = (HashTableClass.Class1)(ht[j]);
textBox1.Text = obj.Roll.ToString();
textBox2.Text = obj.Name;
}
//To Add a Record
private void AddRecord_Click_1(object sender, EventArgs e)
{
HashTableClass.Class1 obj = new HashTableClass.Class1();
obj.Roll = Int32.Parse(textBox1.Text);
obj.Name = textBox2.Text;
key = b;
ht.Add(key, obj);
b = b + 1;
textBox1.Text = "";
textBox2.Text = "";
}
//To Reach on First Record
private void FirstRecord_Click(object sender, EventArgs e)
{
if (j == 1)
{
MessageBox.Show("You All Ready first Record");
}
else
{
j = 1;
HashTableClass.Class1 obj = (HashTableClass.Class1)(ht[j]);
textBox1.Text = obj.Roll.ToString();
textBox2.Text = obj.Name;
}
}
//To Reach on Next Record
private void NextRecord_Click(object sender, EventArgs e)
{
if (j == ht.Count)
{
MessageBox.Show("You All Reday Last Record");
}
else
{
j = j + 1;
HashTableClass.Class1 obj = (HashTableClass.Class1)(ht[j]);
textBox1.Text = obj.Roll.ToString();
textBox2.Text = obj.Name;
}
}
// To Reach on Previous Record
private void Previous_Click(object sender, EventArgs e)
{
if (j == 1)
{
MessageBox.Show("You All Ready On First Record ");
}
else
{
j = j - 1;
HashTableClass.Class1 obj = (HashTableClass.Class1)(ht[j]);
textBox1.Text = obj.Roll.ToString();
textBox2.Text = obj.Name;
}
}
//To Reach on Last Record
private void Last_Click(object sender, EventArgs e)
{
if (j == ht.Count)
{
MessageBox.Show("AlReady At Last");
}
else
{
j = ht.Count;
HashTableClass.Class1 obj = (HashTableClass.Class1)(ht[j]);
textBox1.Text = obj.Roll.ToString();
textBox2.Text = obj.Name;
}
}
}
}
When user run the application then the window will become look like as:

Figure 2: By clicking AddRecord button we can add the record in Hashtable.

Figure 3: After adding the record if user want to see the record by passing the key in textbox.

Figure 4: If user click on first record.

Figure 5: If user click on next record.

Figure 6: If user click on last record.

Figure 7: If user is on last record and he/she click again on last then a message box will show the message that you allready on last record. This functionality is also in first, next, previous button.

Figure 8: If user click on previous record.