Home > DeveloperSection > Blogs > Collection in C#

Collection in C#


.NET C#  Collection 
Ratings:
0 Comment(s)
 617  View(s)
Rate this:

Collection in C#

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

 What is 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.

Example:

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:

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

{

    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:

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

{

    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:

5

4

3

2

1


Don't want to miss updates? Please click the below button!

Follow MindStick