Monday, 12 January 2015

Generics - C#

First of all, let's take an example where generics can be of great help.
Suppose you are writing a class for Stack with methods Push() and Pop(). You have to implement this class in such a way that it works for any data type. Now you will have two options if generic was not there to help you.
  • Define classes for each data types. You can achieve the solution using this approach. However there are many disadvantages of this approach:
    1. You are writing redundant code.
    2. If you need to support a new data type, then you need to add a new class for that.
    3. You are discouraging code reusability.
  • Defined class for data type as object. This is somewhat better than the first approach. However, it has its own disadvantages.
    1. Performance would be degraded because of boxing and un-boxing.
    2. TypeSafety is lost. Your solution will not be type safe.
Generic solves all the above problems. It:
  • allows you to define type-safe data structures, without providing actual data types.
  • allows you to write a class or method that can work with any data type.

Generic Constraints

As you know, you can use any data type with generic classes. Like you can use intfloatdouble data types with generic Stack class. However, sometimes we need to restrict the data types which can be used with particular generic class or methods. We can achieve this using generic constraints.

Generic Constraints are Categorized as Below

Derivation constraints – Restrict the generic type parameter to be derivative of specified interface or class.

public class LinkedList<K,T> where K : IComparable
// K data type must implement IComparable

public class LinkedList<K,T> where K : IComparable<K>
// K data type must implement IComparable<T>

public class LinkedList<K,T> : IEnumerable<T> where K : IComparable<K>
// Derive from IEnumerable<T> and K data type must implement IComparable<K>

public class LinkedList<K,T> where K : IComparable<K>,IConvertible
// K data type must implement IComparable<K> and IConvertible

public class LinkedList where K : IComparable,IConvertible
// K data type must implement both IComparable and IConvertible

Example : 

using System;
using System.Collections.Generic;

namespace GenericApplication
{
    public class MyGenericArray<T>
    {
        private T[] array;
        public MyGenericArray(int size)
        {
            array = new T[size + 1];
        }
        public T getItem(int index)
        {
            return array[index];
        }
        public void setItem(int index, T value)
        {
            array[index] = value;
        }
    }
          
    class Tester
    {
        static void Main(string[] args)
        {
            //declaring an int array
            MyGenericArray<int> intArray = new MyGenericArray<int>(5);
            //setting values
            for (int c = 0; c < 5; c++)
            {
                intArray.setItem(c, c*5);
            }
            //retrieving the values
            for (int c = 0; c < 5; c++)
            {
                Console.Write(intArray.getItem(c) + " ");
            }
            Console.WriteLine();
            //declaring a character array
            MyGenericArray<char> charArray = new MyGenericArray<char>(5);
            //setting values
            for (int c = 0; c < 5; c++)
            {
                charArray.setItem(c, (char)(c+97));
            }
            //retrieving the values
            for (int c = 0; c< 5; c++)
            {
                Console.Write(charArray.getItem(c) + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following result:
0 5 10 15 20
a b c d e

Generic Methods

In the previous example, we have used a generic class; we can declare a generic method with a type parameter. The following program illustrates the concept:


using System;
using System.Collections.Generic;

namespace GenericMethodAppl
{
    class Program
    {
        static void Swap<T>(ref T lhs, ref T rhs)
        {
            T temp;
            temp = lhs;
            lhs = rhs;
            rhs = temp;
        }
        static void Main(string[] args)
        {
            int a, b;
            char c, d;
            a = 10;
            b = 20;
            c = 'I';
            d = 'V';

            //display values before swap:
            Console.WriteLine("Int values before calling swap:");
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine("Char values before calling swap:");
            Console.WriteLine("c = {0}, d = {1}", c, d);

            //call swap
            Swap<int>(ref a, ref b);
            Swap<char>(ref c, ref d);

            //display values after swap:
            Console.WriteLine("Int values after calling swap:");
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine("Char values after calling swap:");
            Console.WriteLine("c = {0}, d = {1}", c, d);
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following result:
Int values before calling swap:
a = 10, b = 20
Char values before calling swap:
c = I, d = V
Int values after calling swap:
a = 20, b = 10
Char values after calling swap:
c = V, d = I

Generic Delegate

You can define a generic delegate with type parameters. For example:
delegate T NumberChanger<T>(T n);

The following example shows use of this delegate:

using System;
using System.Collections.Generic;

delegate T NumberChanger<T>(T n);
namespace GenericDelegateAppl
{
    class TestDelegate
    {
        static int num = 10;
        public static int AddNum(int p)
        {
            num += p;
            return num;
        }

        public static int MultNum(int q)
        {
            num *= q;
            return num;
        }
        public static int getNum()
        {
            return num;
        }

        static void Main(string[] args)
        {
            //create delegate instances
            NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
            NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
            //calling the methods using the delegate objects
            nc1(25);
            Console.WriteLine("Value of Num: {0}", getNum());
            nc2(5);
            Console.WriteLine("Value of Num: {0}", getNum());
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following result:
Value of Num: 35
Value of Num: 175

Features of Generics

Using generics is a technique that enriches your programs in the following ways:
  • It helps you to maximize code reuse, type safety, and performance.
  • You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. You may use these generic collection classes instead of the collection classes in the System.Collectionsnamespace.
  • You can create your own generic interfaces, classes, methods, events and delegates.
  • You may create generic classes constrained to enable access to methods on particular data types.
  • You may get information on the types used in a generic data type at run-time by means of reflection.

0 comments:

Post a Comment

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (9) Web Service (1) XMl (1)