---
title: "Collection in C#"  
description: "In this blog, I’m explaining about Collection in C#  What is a collection?Collection classes are specialized classes for data storage and retrieval. T"  
author: "priyanka kushwaha"  
published: 2015-03-04  
updated: 2018-02-22  
canonical: https://www.mindstick.com/blog/827/collection-in-c-sharp  
category: ".net"  
tags: ["c#", "collection"]  
reading_time: 3 minutes  

---

# Collection in C#

In this blog, I’m explaining about [Collection in C#](https://www.mindstick.com/blog/11208/basic-concept-of-collection-in-c-sharp)

##### \

##### What is a collection?

Collection classes are [specialized](https://yourviews.mindstick.com/audio/1209/caregivers-support-personnel-s-function-in-specialized-disability-accommodation) classes for [data storage](https://www.mindstick.com/interview/2601/what-are-different-data-storage-options-are-available-in-android) and retrieval. These classes provide support for stacks, queues, lists and hash tables.

##### System.Collections collection:

##### \

The classes in System.collection [namespace](https://www.mindstick.com/articles/12552/namespace) allows storing [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-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](https://www.mindstick.com/forum/160391/explain-the-difference-between-generic-classes-and-non-generic-classes-in-c-sharp). The following tabular lists some of the frequently used classes in the System.Collections namespace.

**[ArrayList](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp):** Represents an array of objects whose size is dynamically increased as required.

**Hashtable:** Represents a collect of key & value pair that are [organized](https://yourviews.mindstick.com/story/4668/regions-rivers-where-kumbha-mela-is-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{    class Program    {        static void Main(string[] args)        {            int[] arr = new int[4] { 1, 2, 3, 4 };            string[] str = { "Pawan", "Priyanka", "Kamlakar" };            string name = "Pawan";            //New ArrayList            ArrayList arrlist = new ArrayList();            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:**\

```
PawanPriyankaKamlakar1234Pawan
```

##### \

##### Example of HastTable:

##### \

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace HashDemo{    class Program    {        static Hashtable getHashData()        {            Hashtable hsttbl = new Hashtable();            string str;            for(int i=0;i<3;i++)            {                str=Console.ReadLine();                hsttbl.Add(i, str);            }            return hsttbl;        }        static void 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{    class Program    {        static void Main(string[] args)        {            ArrayList list = new ArrayList();            String[] str=new String[5];                        for (int i = 0; i <str.Length; i++)            {               Console.WriteLine("enter  string");               str[i]=Console.ReadLine();            }            //New Queue             Queue<string> que = new Queue<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:**

```
12345
```

##### \

##### Example of Stack:

##### \

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

**Output:**\

```
54321
```

---

Original Source: https://www.mindstick.com/blog/827/collection-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
