Generics in .NET

Generics were introduced with the .NET framework 2.0 and have now become one of the most important and useful features of .NET. Generics use type parameters to specify the type argument when the type is instantiated. The client can specify the type parameter for the generic type while instantiating the type. So this causes reuse of code since the same generic code can be used for various types using type parameters. Using generics we can create generic interfaces, classes, methods, events and delegates.

Apart from code reuse, another very important use of generics is type safety since type errors are caught at compile time rather than at runtime. Generics are hugely used in collection classes. The System.Collections.Generic namespace includes several generic collection classes. In most cases we should use the generic collection classes in the System.Collections.Generic namespace instead of the non-generic classes in the System.Collections namespace.
We will first see how to use the standard List generic type then we see how to implement a generic class.

Using existing Generic collection class

We use the generic collection class List in the System.Collections.Generic namespace. The List class is the generic counterpart of the non-generic ArrayList class. The List class uses a type parameter that is specified when the List is instantiated and it is a type error to use the type for any other purpose than the type specified at the time of List instantiation.

So the following code works correctly:

image1.png

While the following code gives a compilation error because of type mismatch between integer and string types.

image2.png

So we get type safety by default when using generic types. If we had used the non generic ArrayList class instead of the generic List class we would have not gotten the compile time error but the run time error. Another advantage we got by using the generic type is that type casting and boxing is not required so this results in more optimized code.

Creating a new Generic class

We can create new Generic classes using type parameters. We specify the type parameter in angular brackets (< >) while declaring the class.

The following code is used to create a new generic class:

public class GenericType<T>
{
     T obj;       
     
// T used in non-generic constructor.
     public GenericType(T t)
     {
          obj = t;
     } 
     public T GetObj()
     {
          return obj;
     }
}

The identifier T is called the type parameter and is used in the generic types to specify various types at the time when the type is instantiated. It is used in the class as a placeholder for the exact type specified when the type is created.

In the code above we have declared the variable of type parameter t as the class variable.

We create a new instance of the preceding generic class using the following code:

GenericType<string> gnericObj = new GenericType<string>("ashish");
Console.WriteLine(gnericObj.GetObj());

The output of executing the code above is "ashish". The GetObj() method above simply returns the value of the parameter passed in the constructor.

Up Next
    Ebook Download
    View all
    Learn
    View all