Introduction
It is just like as array ,but always
dynamic and can hold any values unlike array. It is a data structure that holds
data in many different ways. The collection class is defined in the
System.Collections namespace. There are five types of collections in data
structure.
1.Hastable
2.Arraylist
3.Linkedlist
4.Stack
5.Queue
But mostly use of two types collections in C#,they are Hastable and Arraylist.
Here we will discuss about only Hastable and Arraylist.
1. Hastable
Hastable have two values i.e. key(id) and item(value).
Why we use Hastable
If we want to stored data in two dimensional array at run time then we use Hastable. For example, a months name and his serial number and we can access there data using a key.
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
hastable
{
public partial
class Form1
: Form
{
public Form1()
{
InitializeComponent();
}
private void
Form1_Load(object sender,
EventArgs e)
{
}
private void
button1_Click(object sender,
EventArgs e)
{
Hashtable hs =
new Hashtable();
hs.Add("1",
"January");
hs.Add("2",
"Febuary");
hs.Add("3",
"March");
hs.Add("4",
"April");
hs.Add("5","May");
hs.Add("6",
"june");
hs.Add("7",
"July");
hs.Add("8",
"August");
hs.Add("9",
"September");
hs.Add("10",
"October");
hs.Add("11",
"November");
hs.Add("12",
"December");
//MessageBox.Show(hs["8"].ToString());//
print one value(August)
foreach
(string k in
hs.Keys)
{
MessageBox.Show(hs[k].ToString());//print
all values
}
}
}
}
Now run the application.
Output (One value using key) looks like the below image.

Output (all values using foreach loop) looks like the below image.

code description


0
Shahbaz HussainPosted Jul 18, 2019, 3:08 PM
A Good explanation
Chandu KumawatPosted Nov 26, 2015, 7:44 AM
good one
Tausif AlamPosted Apr 3, 2013, 5:25 AM
Thanks Vineet for explaining the difference between array list and hashtable in a simple way.
Mohanraj ManiPosted Apr 3, 2013, 1:18 AM
thanks and i need in depth to develop for collections please