Generic Collection in C#

By
Generic Collection is the concept  which is introduce in 2.0 in C#.By using this concept we can create such class,method,interface,delegate and structure which take Type as Parameter.With the help of this  Generic collection we can easily  work any Data type without any type conversion. In Generic collection there is no need of Boxing and Unboxing of the values . Values will store in a specific type.
Example: List,Dictionary,Stack,Queue etc.
There are two main Example of Generic collection which are given below:

1.Generic Method :

In Generic method  we use angular bracket(<p>) for Parameter Type .You can easily pass any Data Type at Run time to the Function(Method) in the program.

see it:

using System;
using System.Collections.Generic;
namespace Generic
{
    class Program
    {
        public class cls
        {
            public void show<p>(ref p i, ref p j)
            {
                p val = i;
                i = j;
                j = val;
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the two values ");
            int val1 = int.Parse(Console.ReadLine());
            int val2 = int.Parse(Console.ReadLine());
            cls obj = new cls();
            obj.show<int>(ref val1, ref val2);
            Console.WriteLine("after reversing  values are:");
            Console.WriteLine(val1);
            Console.WriteLine(val2);
            String str1 = "Hello";
            String str2 = "csharp";
            obj.show<String>(ref str1, ref str2);
            Console.WriteLine(str1);
            Console.WriteLine(str2);
            Console.ReadLine();
        }
    }
}

Output:

Description:- Here i have First Declared a show() Method with a optional parameter(<p>)
ex. public void show<p>(ref p i, ref p j).After that i have created the object of the class in main method.With the help of class object and method(show()) ,i have directly passed integer value and String value to the method(show()) through Main method without any Type conversion which is given below:
obj.show<int>(ref val1, ref val2);
obj.show<String>(ref str1, ref str2);
It is like as Template in c++ language.

2. Generic Class:-

 In Generic class  we use angular bracket(<>) for Parameter Type.Here we pass parameter to the class not method,which is shown in the example:
see it:-

using System;
using System.Collections.Generic;
namespace Generic_class
{
    class Program
    {
        public class cls<p>       
 {
            public void show(ref p i, ref p j)
            {
                p val = i;
                i = j;
                j = val;
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the two values ");
            int val1 = int.Parse(Console.ReadLine());
            int val2 = int.Parse(Console.ReadLine());
            cls<int> obj =  new cls<int>();
            obj.show(ref val1, ref val2);
            Console.WriteLine("after reversing  values are:");
            Console.WriteLine(val1);
            Console.WriteLine(val2);
            Console.ReadLine();
        }
    }
}

Output:

Description:- It is same like method but here i have passed parameter Type in class not method.At the class object  creation time i have mentioned the Integer value,so that compiler know that class is taking Integer Type parameter.so we can directly pass the integer value to method and compiler does not give any error.

There are some other Example of Generic class in c# which are using above two Example concepts.which is given below:
     1. List:

using System;
using System.Collections.Generic;
namespace List
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> obj = new List<int>();
            obj.Add(12);
            obj.Add(23);
            obj.Add(34);
            obj.Add(100);
            Console.WriteLine("List element is:");
            foreach (int i in obj)
            {
                Console.WriteLine(i);
            }
            obj.Remove(12);
            obj.Insert(2, 200);
            Console.WriteLine("After Removing and inserting List element is: ");
            foreach (int j in obj)
            {
                Console.WriteLine(j);
            }
            Console.ReadLine();
        }
    }
}

Output:

      2. Stack:

using System;
using System.Collections.Generic;
namespace Stack
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<String> obj =new Stack<String>();
            obj.Push("Ram");
            obj.Push("abc");
            obj.Push("rajesh");
            obj.Push("Anil");
            Console.WriteLine("Stack element is:");
            foreach (String m in obj)
            {
                Console.WriteLine(m);
            }
           obj.Pop();
           Console.WriteLine("After Removing Stack element is:");
           foreach (String m in obj)
           {
               Console.WriteLine(m);
           }
            Console.ReadLine();
        }
    }
}

Output:

     3. Queue:

using System;
using System.Collections.Generic;
namespace queue
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue<int> obj = new Queue<int>();
            obj.Enqueue(13);
            obj.Enqueue(30);
            obj.Enqueue(10);
            obj.Enqueue(50);
            Console.WriteLine("Element in queue ");
            foreach (int m in obj)
            {
                Console.WriteLine(m);
            }
            obj.Dequeue();
            Console.WriteLine("After dequeue Element in queue is");
            foreach (int x in obj)
            {
                Console.WriteLine(x);
            }
            Console.ReadLine();
        }
    }
}

Output:

4. Dictionary:

using System;
using System.Collections.Generic;
namespace dictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int,String> dictobj = new Dictionary<int,String>();
            dictobj.Add(10, "RAM");
            dictobj.Add(20, "Mohan");
            dictobj.Add(30, "Rajesh");
            dictobj.Add(50, "Sunil");
            Console.WriteLine("Dictionary elements are:");
            Console.WriteLine(dictobj[10]);
            Console.WriteLine(dictobj[20]);
            Console.WriteLine(dictobj[50]);
            Console.WriteLine(dictobj[30]);
            Console.ReadLine();
        }
    }
}

Output:


     3. Specialized Collection:-

  Specialized collection is store the data within 'String Type'.Specialized collection is used in  .NET Framework  for specific purpose.This collection classes is used less compare to other collection classes.These collection classes comes under Namespace: using System.Collections.Specialized
Example:
The Name Value collection:
These classes stores the value in string pairs.In this collection multiple values is stored in a Single key.It returns as an array of string.
For More

  1. Collection in C#
  2. Arrays in C#

I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application
           Download

0 comments:

Post a Comment

Powered by Blogger.