Generics were introduced in the version 2.0 of the .NET framework.Generics allows the user to specify the data type when the generic type is declared and initialized.
Generics work with different data types
Generics such as generic method or generic class accepts type parameters and hence work on different data types.This means that we can create different versions of a single generic type by supplying different data types as type arguments when declaring and instantiating a generic type.For example we can create a string and int versions of a generic class.
So generics provide reusability as we declare just one version of a generic type and then use it for different data types.
Defining and using Generics
Generic class
We can define a generic type using the type parameters which are placeholders for the
actual types which are substituted when the generic type is defined and initialized.
Following are the steps to declare and use a generic type
Declare a generic class using type parameters
public class GenericClass<T> { private T property public T Property { get { return property; } set { property=value; } } }
Above we are declaring a Generic class called GenericClass which uses a type parameter called T.We can identify a generic class from the “<>” brackets after the class name.
The name specified inside the “<>” brackets is the type parameter.The type parameter represents
a data type such as int which is supplied when the generic class object is declared.Also the type
parameter can be used anywhere inside the class where any data type can be used such as
when declaring fields and properties.
Creating a generic class object
GenericClass<int> obj=new GenericClass<int>();
In the above code we are passing type argument int to the GenericClass type.So now we have an int version of the GenericClass type.
Generic methods
The difference between a generic method and a normal method is that a generic method accepts type parameters.
static void GenericMethod<T>(T parameter1) { T variable1; }
In the above method we have declared a generic method called GenericMethod which accepts a type parameter T which is a placeholder for the actual data type.
We can call the above generic method as
int a; GenericMethod<int>(a);
we can also omit the type argument while calling the generic method as
int a; GenericMethod(a);
We do not supplied the type argument above as the compiler was able to infer the type parameter from the type of the supplied argument.Since type argument “a” is of type int hence compiler inferred the type parameter as an int.
It is important to understand few things about generic methods
- We can declare a generic method inside a non-generic class.
- Generic methods can be overloaded based on the type parameter.So we can have the following generic methods inside a non-generic class
void Method1<T>() { } void Method2<T, U>() { }
Generic Interface
Generic interface is just like a normal interface except that it includes the type parameter
public interface IGeneric<T> { }
We can use the type parameter inside the generic interface as:
public interface IGeneric<T> { void Method1(T t1); }
Advantages of Generics
Generics provides the following advantages over non-generic types
- Since the same generic type can work with different data types so the same code can be reused across the different data types
- As the type parameters are type checked at compile time rather than runtime so errors are detected early on during the compilation stage
- Type casting is not required since the generic type is created for a particular type specified by the type parameter.This makes the code more efficient.For example if we use the generic object type for storing different datatypes then we need to use type casting or boxing/unboxing.
In the following example we are using both object type and the generic type for storing items in a stack.
Using the object type to store different items in a stack
object s = new object(); s.Push("1"); s.Push(2);
Using the generic type to store items in a stack
Stack<int> genericStack = new Stack<int>(); genericStack.Push("1"); genericStack.Push(2);
In the examples above the difference between the non-generic and generic version of the stack is :
In the first example a string value is stored in the stack.As a consequence of this while retrieving the value an error would be thrown if the incorrect cast used.In the second example even though we are trying to store a string value in the stack we will get a compile time error as the stack is for the int type.
Also in the second example we don’t need to cast the stored items to int type as the stack is for int type.This makes the code more efficient as compared to the first example.
So as we have seen generics in C# helps in writing code which works with different data types and also the code is more optimized then the non generic version.Also run time errors because of data type mismatch do not occur in the case of generics.
Leave a Reply