Search This Blog

Oct 18, 2012

Generics classes Overview in C#.Net

Generics main benefit is eliminating boxing and unboxing that is true, there are also other benefits like type safety, by using generics you eliminate type ambiguitiy.

  1. Generics provide type safety without the overhead of multiple implementations. Ex. We can create a linked list of string. LinkedListlinkList=new LinkedList(); There is no need to inherit from a base type and override members.The linked list is ready for immediate use.
  2. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced.
  3. Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types
  4. Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.  

  1. Generic classes and methods combine reusability, type safety and efficiency.
  2. Generics are most commonly used with collections and the methods.
  3. VS.NET Version 2.0 of the .NET Framework class library provides a new namespace, System.Collections.Generic; which contains several new generic-based collection classes.

Powerful Uses 

1: Maximize The code reuse.
2. Type safety.
3. Performance etc.


The  type parameter T in angle brackets
public class GenericList<T>
    // The nested class is also generic on T
    private class Node
        // T used in non-generic constructor
        public Node(T t)
            next = null;
            data = t;
        private Node next;
        public Node Next
            get { return next; }
            set { next = value; }
        // T as private member data type
        private T data;
        // T as return type of property
        public T Data
            get { return data; }
            set { data = value; }
    private Node head;
    // The constructor
    public GenericList()
        head = null;

    // T as method parameter type:
    public void AddHeadMethod (T t)
        Node nObject = new Node(t);
        nObject .Next = head;
        head = nObject ;

    public IEnumerator<T> GetEnumerator()
        Node current = head;
        while (current != null)
            yield return current.Data;
            current = current.Next;

// 1: The  type of a method parameter in the AddHeadMethod method.
// 2: The return type of the public method GetNext and the Data property in the nested Node class.
// 3: Thetype of the private member data in the nested class.
// Declare the generic class

public class GenericList<T>
    void Add(T input) { }
class TestGenericList
    private class ExampleClass { }
    static void Main()
        // Declare a list of type int        GenericList<int> gObjectList1 = new GenericList<int>();

        // Declare a list of type string
        GenericList<string> gObjectList2= new GenericList<string>();

        // Declare a list of type Example Class
        GenericList<ExampleClass> gObjectList3= new GenericList<ExampleClass>();

/A generic method is a method that is declared with type parameters, as follows:

static void Swap<T>(ref T a, ref T b)
    T tempT;
    temp = a;
    a = b;
    b = tempT;
The following code example shows one way to call the method, using int for the type argument:

public static void TestSwapMethod()
    int a = 1;
    int b = 2;
    Swap<int>(ref a, ref b);
    System.Console.WriteLine(a + " " + b);
If you define a generic method that takes the same type parameters as the containing class the compiler will generate warning message because within the method scope, the argument supplied for the inner T will hide the argument supplied for
the outer T.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...