Demystifying C# Generics
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Generics have been added to the .Net framework since version 2.0. They introduce the type parameter T to methods, classes (or types) and interfaces.
In programming, generics are also known as parametrized types or parametric polymorphism.
The concept of generics is not new; It has been there since 1973 when it was first pioneered by a programming language called ML, and throughout the years generics were part of many programming languages including ADA, Java, Delphi, Rust and Huskell. In C++ generics are known as templates, and the STL (Standard Template Library) relies heavily on generics. For further and in-depth details regarding generics in programming languages, Wikipedia has a good and in-depth read about generic programming.
And also there is a documented conversation about Generics in C#, Java and C++ with Anders Hejlsberg (The original author of Turbo Pascal , the chief architect of Delphi and the current lead architect of C#). I highly recommend you read this conversation.
With using generics, your code will become reusable, type safe (and strongly-typed) and will have a better performance at run-time since, when used properly, there will be zero cost for type casting or boxing/unboxing which in result will not introduce type conversion errors at runtime. The compiler of C# will be able to detect any problems of invalid casting at compile time.
Reusability with generics means that you will be able to create a method or a class that can be reused with different types in several places.
Now with generics you will be writing only one method, that accepts parameter of type
T, and inside this method it will do the needed implementation, and it can either return void, or a concrete value (String, int, Book, …etc) or even
Type safety and better performance both come together because at compile time the c# compiler will give errors and not compile whenever it knows there will an unsafe casting, and at runtime there will be no casting from type to type, it will be handled naturally.
Now this is true that you will have a code reusability with have having a single method that accepts and object type, and the Type you are passing, but you will be paying the cost of boxing/unboxing.
Boxing / Unboxing
Boxing is the process of converting a type to object (or as the name suggests wrapping it inside the object container.
Unboxing means converting the object to a type. or unwrapping the type from the object container.
Boxing/Unboxing are costly operations, and it is always better to not rely on them heavily in your code.
With Generics you avoid doing boxing/unboxing since you are dealing with the parameter type
T, which is a natural parameter that the compiler will replace it with the concrete type at compilation time without doing the boxing operation at runtime.
By the way, if you would ask why we always use the letter
T it is because by convention it refers to the word Type. You can use whatever valid letter or word you like for generics. The naming rules is no different than for naming classes.
And a last note, you are not tied to only use one parameter, you can specify multiple parameters separating them with comma , like <T, L, K> or whatever valid naming you like. and then you can use the parameter in the place you want to use it.
Now let’s start with explaining more about generics with methods, classes, interfaces, with different examples.