Collections in C#

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.

one-hs.gif

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

all-hs.gif

code description

In the above code first of all we make the object of hastable i.e. hs. After that we add the key and value using the hs object in the hastable. If we print the only one value then we will give your key(id), which is shown above. If we print the all values then we use foreach loop.

2. ArrayList

 Arraylist have only one values i.e. item(value) not a key(id).

Why we use ArrayList

We know that  array have a fixed size. If we want to add items beyond the specified dimension then we use Arraylist. If we want to insert an item in a list then we use Linked list but in the C# we use Arraylist for that purpose.

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 arraylist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList();
            al.Add("January");
            al.Add("Febuary");
            al.Add("March");
            al.Add("April");
            al.Add("May");
            al.Add("june");
            al.Add("July");
            al.Add("August");
            al.Add("September");
            al.Add("October");
            al.Add("November");
            al.Add("December");
           
//MessageBox.Show(al[0].ToString());//Print the one value

            foreach (string k in al)
            {
                MessageBox.Show(k.ToString());// Print the all values
           } 

        }

   }

}

 

Now run the application.
Output (One value using index number) looks like the below image.

ona-ar.gif

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

all-ar.gif0

code description


In the above code first of all we make the object of Arraylist i.e. 'al'. After that we add the value using the 'al' object in the Arraylist. If we print the only one value then we will give index number, which is shown above. If we print the all values then we use foreach loop.

Summary

Collections are very important for stored data in the array and list. Collections solves the many problem like as fixed array ,linked list etc. So collections are very useful for solving these problems.



Ebook Download
View all
Learn
View all