blog

Home / DeveloperSection / Blogs / Basic Concept of Collection in C#

Basic Concept of Collection in C#

Abhishek Srivasatava 1856 20-Sep-2016

Collection in C# or data structure in C# is the ability to hold the data for flexible operation , it is easy to understand, very easy to use and make your program shrink.

There are different concepts in c# to use the collection.

With the help of ArrayList Classes:

The Array List concept is the most flexible and simplest way to  use data structure.

ArrayList is the class which is predefined class in the namespace using System.Collections;     

Syntax : ArrayList Arr =new ArrayList();


Below is the simple program to explain the concept how to insert an element in the ArrayList :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
          
ArrayList Arr = new ArrayList();
          
string UserInput;
Console.WriteLine("\t***********ArrayList Related Program************* ");
Console.WriteLine("Enter number of element ");
            int n= Convert.ToInt32(Console.ReadLine());
 
        
            for(int i = 0; i < n;i++)
            {
                Console.WriteLine("Enter element  ");
UserInput = Console.ReadLine();
Arr.Add(Convert.ToInt32(UserInput));
            }
          
//Dispaying element
            Console.WriteLine("Display element  ");
            foreach(var item in Arr)
            {
Console.WriteLine("\t {0}",item);
            }
 
            //Inserting an Element in the list
          
Arr.Insert(1,2);
           
//Dispaying element
          
Console.WriteLine("Display element After Insert ");
          
foreach (var item1 in Arr)
           {
Console.WriteLine("\t{0}", item1);
            }
          
//Sorting  element
          
Arr.Sort();
          
Console.WriteLine("Display element After Sort ");
          
foreach (var item2 in Arr)
           {
Console.WriteLine("\t{0}", item2);
            }
            //Removing an element
          
Arr.Remove(5);
           
Console.WriteLine("Display element After Removed ");
          
foreach (var item3 in Arr)
             {
Console.WriteLine("\t{0}", item3);
            }
          
Console.ReadLine();
 
        }
    }
}

Output :

Basic Concept of Collection in C#

 Hash Table : 

Hash table is rarely use by the programmer. But this concept is useful to understand old program.

It is the data structure which can accept two values 1st one is key and other is value.

It is like the Container which can accept value with index or identity and put at the first place. When we need to display the list last value will show 1st.

 Basic Concept of Collection in C#

Below is the example to understand the concept related to Hash table

 using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    classProgram
    {
        static void Main(string[] args)
        {
            Hashtable Hrr = newHashtable();
            string UserInput;
Console.WriteLine("\t***********Hash_Table Related Program************* ");
            Console.WriteLine("Enter numbe of element ");
            int n = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i < n;i++)
            {
                Console.WriteLine("Enter element ");
                UserInput = Console.ReadLine(); 
                Hrr.Add(i, UserInput);
            }
            //Dispaying element
            Console.WriteLine("Displayed element  ");
 
            foreach (DictionaryEntry item in Hrr)
            {
                Console.WriteLine("\t{0}\t{1}", item.Key,item.Value);
            }
 
            Hrr.Remove(1);
 
            Console.WriteLine("Display element After Removed ");
 
            foreach (DictionaryEntry item3 in Hrr)
            {
                Console.WriteLine("\t{0}\t{1}", item3.Key, item3.Value);
            }
 
            Console.ReadLine();
 
        }
    }
}


Output

Basic Concept of Collection in C#

Stack

Stack works on the principle of last in- first out. Live example of last in first out is Building Lift. In the lift last person entered in the lift will get the chance to exist first.Stack works on the principle of last in- first out. Live example of last in first out is Building Lift. In the lift last person entered in the lift will get the chance to exist first.

There are two concepts related to Stack Push and Pop.

Push means inserting an element and pop means deleting an element.

 Here is small program related to Stack :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Stack Srr = newStack(); 
            string UserInput;
            Console.WriteLine("\t**********Stack Related Program*************");
            Console.WriteLine("Enter number of element ");
            int n = Convert.ToInt32(Console.ReadLine()); 
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Enter element ");
                UserInput = Console.ReadLine();
                Srr.Push(UserInput);
            }
            //Dispaying element
            Console.WriteLine("Displayed element  ");
            foreach (var item in Srr)
            {
                Console.WriteLine("\t{0}", item);
            }
            Srr.Pop();
            Console.WriteLine("Display element After Removed ");
            foreach (var item3 in Srr)
            {
                Console.WriteLine("\t{0}", item3);
            }
            Console.ReadLine();
        }
    }
}

Output :

Basic Concept of Collection in C#

Queue

Stack works on the principle First in first out. Live example of First in first out are " A queue of people at the ticket-window", "Vehicles on the toll-tax bridge" or "Patients waiting outside the doctor's clinic"

There are two concepts related to Queue, Enqueue and Dequeue.

Enqueue means inserting an element and Dequeue means deleting an element.

Here is small program related to Queue :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Queue Qrr = newQueue();
 
            Console.WriteLine("\t**********Queue Related Program*************");
 
            string UserInput;
 
            Console.WriteLine("Enter number of element ");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Enter element");
                UserInput = Console.ReadLine();
 
                Qrr.Enqueue(UserInput);
            }
 
            //Dispaying element
 
            Console.WriteLine("Displayed element  ");
 
            foreach (var item in Qrr)
            {
                Console.WriteLine("\t{0}", item);
            }
 
            Qrr.Dequeue();
 
            Console.WriteLine("Display element After Removed ");
 
            foreach (var item3 in Qrr)
            {
                Console.WriteLine("\t{0}", item3);
            }
 
            Console.ReadLine();
        }
    }
}

Output :
Basic Concept of Collection in C#


Updated 16-Mar-2018

Leave Comment

Comments

Liked By