blog

Home / DeveloperSection / Blogs / Collection in C#

Collection in C#

priyanka kushwaha2275 04-Mar-2015

In this blog, I’m explaining about Collection in C#


What is a collection?

Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists and hash tables.

System.Collections collection:

The classes in System.collection namespace allows storing elements of any data type in a single collection. Every element is stored as an object of type object. So these collection classes are typically referred as non-generic classes. The following tabular lists some of the frequently used classes in the  System.Collections namespace. 

ArrayList: Represents an array of objects whose size is dynamically increased as required.

Hashtable:Represents a collect of key & value pair that are organized based on the hash code of the key.

Queue: Represents  a first in first out collection of objects.

Stack: Represents a last in first out collection of object.


Examples:


Example of ArrayList:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ArraylistDemo
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            int[] arr = newint[4] { 1, 2, 3, 4 };
            string[] str = { "Pawan", "Priyanka", "Kamlakar" };
            string name = "Pawan";
//New ArrayList
            ArrayList arrlist = newArrayList();
            arrlist.Add(str);
            arrlist.Add(arr);
            arrlist.Add(name);
            foreach (object obj in arrlist)
            {
                Type objtype = obj.GetType();
                if (objtype.IsArray)
                {
                    if (objtype == typeof(System.Int32[]))
                    {
                        int[] strlist = (int[])obj;
                        for (int i = 0; i < strlist.Length; i++)
                            Console.WriteLine(strlist[i]);
                    }
                    else
                    {
                        String[] stringArray = (String[])obj;
                        for (int i = 0; i < stringArray.Length; i++)
                            Console.WriteLine(stringArray[i]);
                    }
                }
                 else
                {
                    Console.Write(obj.ToString());
                }
            }
            Console.ReadKey();
        }
    }
}

Output:

Pawan
Priyanka
Kamlakar
1
2
3
4
Pawan

Example of HastTable:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace HashDemo
{
    classProgram
    {
        staticHashtable getHashData()
        {
            Hashtable hsttbl = newHashtable();
            string str;
            for(int i=0;i<3;i++)
            {
                str=Console.ReadLine();
                hsttbl.Add(i, str);
            }
            return hsttbl;
        }
        staticvoid Main(string[] args)
        {
            Hashtable hasttable = getHashData();
            foreach (DictionaryEntry entry in hasttable)
            {
                Console.WriteLine("{0}{1}",entry.Key,entry.Value);
            }
            Console.ReadKey();
        }
    }
}

 

Example:

1 Priyanka

2 Sita

3 Ram

 

Example of Queue:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace QueueListDemo
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            ArrayList list = newArrayList();
            String[] str=newString[5];
           
            for (int i = 0; i <str.Length; i++)
            {
           Console.WriteLine("enter  string");
             str[i]=Console.ReadLine();
            }
//New Queue
            Queue<string> que = newQueue<string>(str);
            Console.WriteLine("queue representation");
            foreach (var item in que)
              Console.WriteLine(item);
        
 
            Console.WriteLine("Enqueue Example");
            string stren;
            Console.WriteLine("Enter a string");
          stren= Console.ReadLine();
            que.Enqueue(stren);
            Console.WriteLine("Enqueue Representation");
            foreach (var en in que)
                Console.WriteLine(en);
            que.Dequeue();//Remove first element;
            Console.WriteLine("After removal the element in the  queue are:");
            foreach (var en in que)
                Console.WriteLine(en);
            Console.ReadKey();
        }
    }
}

Output:

1
2
3
4
5

Example of Stack:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace QueueListDemo
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            ArrayList list = newArrayList();
            String[] str = newString[5];
 
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine("enter  string");
                str[i] = Console.ReadLine();
            }
 
            Stack<string> stk = newStack<string>(str);
            Console.WriteLine("Stack representation");
            foreach (var item in stk)
                Console.WriteLine(item);
 
            Console.ReadKey();
        }
    }
}

Output:

5
4
3
2
1

Updated 22-Feb-2018

Leave Comment

Comments

Liked By