---
title: "Basic Concept of Collection in C#"  
description: "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"  
author: "Abhishek Srivasatava"  
published: 2016-09-20  
updated: 2018-03-16  
canonical: https://www.mindstick.com/blog/11208/basic-concept-of-collection-in-c-sharp  
category: "c#"  
tags: ["c#", "collection"]  
reading_time: 4 minutes  

---

# Basic Concept of Collection in C#

[Collection in C#](https://www.mindstick.com/blog/827/collection-in-c-sharp) or data [structure in C#](https://www.mindstick.com/forum/33692/how-to-use-structure-in-c-sharp) is the ability to hold the data for flexible operation , it is easy to understand, very easy to use and make [your program](https://answers.mindstick.com/qa/33494/how-do-you-find-any-view-element-into-your-program-explain-for-example) shrink.\

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

**With the help of [ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) Classes:**

The Array List concept is the most flexible and simplest way to use [data structure](https://www.mindstick.com/blog/11221/simple-way-to-learn-dynamic-data-structure-in-c-language).

ArrayList is the class which is predefined class in the [namespace](https://www.mindstick.com/articles/12552/namespace) using System.Collections;

```
Syntax : ArrayList Arr =new ArrayList();
```

**\**

**Below is the simple program to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) 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#](https://www.mindstick.com/blogs/422bd2d0-388a-4419-8c6b-c828b5922cc2/images/f56374a2-20f4-4f05-922f-cf8ce1f64c9d.png)**\**

**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#](https://www.mindstick.com/blogs/422bd2d0-388a-4419-8c6b-c828b5922cc2/images/ef6fa6d7-965b-446e-8465-3c7194fe9cb8.png)

**Below is the example to understand the concept related to [Hash table](https://www.mindstick.com/forum/157906/what-is-the-difference-between-a-hash-table-and-an-array-when-would-you-use-one-over-the-other)**

```
 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)        {            Hashtable Hrr = new Hashtable();            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#](https://www.mindstick.com/blogs/422bd2d0-388a-4419-8c6b-c828b5922cc2/images/5a36c653-9146-42ce-ab6b-98800564d856.png)**\**

**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{    class Program    {        static void Main(string[] args)        {            Stack Srr = new Stack();             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#](https://www.mindstick.com/blogs/422bd2d0-388a-4419-8c6b-c828b5922cc2/images/bb060275-c7e4-4f34-896e-db38a146213f.png)**\**

**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{    class Program    {        static void Main(string[] args)        {            Queue Qrr = new Queue();             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#](https://www.mindstick.com/blogs/422bd2d0-388a-4419-8c6b-c828b5922cc2/images/20860e62-be6b-4f3d-881c-305b0e1f34ee.png)

---

Original Source: https://www.mindstick.com/blog/11208/basic-concept-of-collection-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
