Sunday 3 February 2013

Generics in C# .NET


C# 2.0 introduces new functionality named Generics. These allow us to create classes and methods decoupled from the data types. This means that using Generics we can create generalized classes and methods.
Generic classes are available in System.Collections.Generic namespace.

Example of generic methods

using System;
using System.Collections.Generic;

namespace GenericsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Equal method for numbers only
            EqualNumber objEqualNumber = new EqualNumber();
            Console.WriteLine("Number Equals: " + objEqualNumber.IsEqual(5, 6));

            //Equal method for string only
            EqualString objEqualString = new EqualString();
            Console.WriteLine("String Equals: " + objEqualString.IsEqual("Hello", "Hello"));

            //Equal method for both number and string using object parameter
            //This method use boxing and unboxing
            EqualObject objEqualObject = new EqualObject();
            Console.WriteLine("Number Equals: " + objEqualObject.IsEqual(10, 10));
            Console.WriteLine("String Equals: " + objEqualObject.IsEqual("Hello","Hello"));

            //This code will compile and execute successfully it is not type safety
            Console.WriteLine("String Equals: " + objEqualObject.IsEqual("Hello", 10));

            //This use Generics methods to compare equallity using strong type safety
            //This method dose not use boxing and unboxing
            EqualGenerics objEqualGenerics = new EqualGenerics();
            Console.WriteLine("Number Equals: " + objEqualGenerics.IsEqual<int>(10, 10));
Console.WriteLine("String Equals: " + objEqualGenerics.IsEqual<string>("Hello","Hello"));
           
            Console.ReadLine();
        }
    }

    //objects
    public class EqualObject
    {
        public bool IsEqual(object value1, object value2)
        {
            return value1.Equals(value2);
        }
    }

    //number
    public class EqualNumber
    {
        public bool IsEqual(int value1, int value2)
        {
            return value1.Equals(value2);
        }
    }

    //string
    public class EqualString
    {
        public bool IsEqual(string value1, string value2)
        {
            return value1.Equals(value2);
        }
    }

    //Generics
    public class EqualGenerics
    {
        public bool IsEqual<T>(T value1, T value2)
        {
           return value1.Equals(value2);
        }
    }
}


Generic Interface


      IInterface.cs

    using System.Collections.Generic;



namespace ConsoleApplication1
{
     interface IInterface<T>
    {
        List<T> Display(List<T> lst);
    }

    class ClassA : IInterface<Person>
    {
        public List<Person> Display(List<Person> lstPerson)
        {
            List<Person> lstPerson1 = new List<Person>();
            Person objPersion = new Person();
            objPersion.Firstname = "Rahul";
            lstPerson1.Add(objPersion);
            return lstPerson1;
        }
    }

     class ClassB : IInterface<Employee>
    {
        public List<Employee> Display(List<Employee> lstEmployee)
        {
            List<Employee> lstEmployee1 = new List<Employee>();
            Employee objEmployee = new Employee();
            objEmployee.EmployeeName = "Markandey";
            lstEmployee1.Add(objEmployee);
            return lstEmployee1;
        }
    }



    public class Person
    {
        public string Firstname { get; set; }
    }

    public class Employee
    {
        public string EmployeeName { get; set; }
    }
}


Program.cs

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> lstPerson = new List<Person>();
            ClassA objA = new ClassA();
            lstPerson = objA.Display(lstPerson);

            List<Employee> lstEmployee = new List<Employee>();
            ClassB objB = new ClassB();
            lstEmployee = objB.Display(lstEmployee);

            Console.ReadLine();
        }
    }
}

Advantage of Generics
·         Use generic types to maximize code reuse, type safety, and performance.
·         The most common use of generics is to create collection classes.
·         The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace.
·         You can create your own generic interfaces, classes, methods, events and delegates.
·         Generic classes may be constrained to enable access to methods on particular data types.
·         Information on the types used in a generic data type may be obtained at run-time by means of reflection.




No comments:

Post a Comment