articles

Home / DeveloperSection / Articles / Collection in C#

Collection in C#

Sumit Kesarwani 4222 11-May-2013

Collection classes are used for data storage and manipulate the data.Collections are data structures that holds data in different ways for flexible operations. C# Collection classes are defined as part of the System.Collections or System.Collections.Generic namespace.

Main collection classes which are used in c#

  •  ArrayList Class
  • List Class
  • HashTable Class
  • Stack Class
  • Queue class 
ArrayList

ArrayList is a collection from a standard System.Collections namespace. It is a dynamic array. It provides random access to its elements. An ArrayList automatically expands as data is added. Unlike arrays, an ArrayList can hold data of multiple data types. Elements in the ArrayList are accessed via an integer index. Indexes are zero based. Indexing of elements and insertion and deletion at the end of the ArrayList takes constant time. Inserting or deleting an element in the middle of the dynamic array is more costly. It takes linear time.

Example
using System;

using System.Collections;
 
namespace ArrayListConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al=new ArrayList();
            Console.WriteLine("Example OF ArrayList\n");
            char ch='y';
            while(ch=='y')
            {
                Console.Write("Enter Data=");
                int data=Convert.ToInt32(Console.ReadLine());
                al.Add(data);
                Console.Write("\nAdd more data to ArrayList(y/n) = ");
                ch=Convert.ToChar(Console.ReadLine());
            }
 
            Console.WriteLine("\nData In ArrayList");
            foreach (intiinal)
            {
                Console.Write(i+"\t");
            }
            Console.ReadKey();
        }
    }
}

 Output

Collection in C#

List

The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required. Elements in this collection can be accessed using an integer index. The List is not guaranteed to be sorted. You must sort the List before performing operations (such as Binary Search) that require the List to be sorted.

Example
using System;

using System.Collections.Generic;
 
namespace ListConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string>mylist=newList<string>();
            Console.WriteLine("Example of List");
            for (inti=1; i<=5; i++)
            {
                Console.Write("Enter Conutry = ");
                string country=Console.ReadLine();
                mylist.Add(country);
            }
            Console.WriteLine("\nData In List");
            foreach (stringiinmylist)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
    }
}

 Output

Collection in C#

HashTable

Hashtable in C# represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can retrieve items from hashTable to provide the key . Both keys and values are Objects. 

Stack

The Stack class represents a last-in-first-out (LIFO) Stack of Objects. Stack follows the push-pop operations that are we can Push (insert) Items into Stack and Pop (retrieve) it back. Stack is implemented as a circular buffer. It follows the Last In First Out (LIFO) system that is we can push the items into a stack and get it in reverse order. Stack returns the last item first. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.

Example
using System;

using System.Collections;
 
namespace StackConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st=new Stack();
            Console.WriteLine("Example Of Stack\nPush Data in Stack");
            char ch='y';
            while (ch=='y')
            {
                Console.Write("Enter Data=");
                string data=Console.ReadLine();
                st.Push(data);
                Console.Write("Do you want to continue(y/n)=");
                ch=Convert.ToChar(Console.ReadLine());
            }
            Console.WriteLine("\nCurrent Stack");
            foreach (stringiinst)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("Do you want to delete data from stack(y/n)");
            ch=Convert.ToChar(Console.ReadLine());
            while (ch=='y')
            {
                st.Pop();
                Console.WriteLine("\nCurrent Stack");
                foreach (string iinst)
                {
                    Console.WriteLine(i);
                }
                Console.WriteLine("Do you want to delete data from stack(y/n)");
                ch=Convert.ToChar(Console.ReadLine());
            }
            Console.ReadKey();
        }
    }
}

 Output

Collection in C#

Queue

The Queue works like FIFO system , a first-in, first-out collection of Objects. Objects stored in a Queue are inserted at one end and removed from the other. The Queue provides additional insertion, extraction, and inspection operations. We can Enqueue (add) items in Queue and we can Dequeue(remove from Queue ) or we can Peek (that is we will get the reference of first item ) item from Queue. Queue accepts null reference as a valid value and allows duplicate elements.

Example
using System;

using System.Collections.Generic;
 
namespace QueueConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue<string>myqueue= newQueue<string>();
            Console.WriteLine("Example Of Queue\nEnter Data In Queue");
            char ch='y';
            while (ch=='y')
            {
                Console.Write("Enter Data =");
                string data=Console.ReadLine();
                myqueue.Enqueue(data);
                Console.Write("Do you want to continue(y/n)=");
                ch=Convert.ToChar(Console.ReadLine());
            }
            Console.WriteLine("\nCurrent Queue");
            foreach (stringiinmyqueue)
            {
                Console.WriteLine(i);
            }
            Console.Write("\nDo you want to delete data from queue(y/n)=");
            ch=Convert.ToChar(Console.ReadLine());
            while (ch=='y')
            {
 
                myqueue.Dequeue();
                Console.WriteLine("\nCurrent Queue");
                foreach (stringiinmyqueue)
                {
                    Console.WriteLine(i);
                }
                Console.Write("Do you want to continue(y/n)=");
                ch=Convert.ToChar(Console.ReadLine());
            }
            Console.ReadKey();
        }
    }
}
Output

Collection in C#


c# c# 
Updated 07-Sep-2019

Leave Comment

Comments

Liked By