C Sharp Programming/Generics

Generics are a new feature available since version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. The most common use of generics is to create collection classes. Generic types were introduced to maximize code reuse, maximizing type safety, and performance.

Generic classes
There are cases when you need to create a class to manage objects of some type, without modifying them. Without generics, the usual approach (highly simplified) to make such class would be like this:

And its usage would be:

Note that we have to cast back to original data type we have chosen (in this case - ) every time we want to get an object from such a container. In such small programs like this, everything is clear. But in more complicated cases with more containers in different parts of the program, we would have to take care that the container is supposed to have type in it and no other data type, as in such a case, a  is thrown.

Additionally, if the original data type we have chosen is a value type, such as, we will incur a performance penalty every time we access the elements of the collection due to the autoboxing feature of C#.

However, we could surround every unsafe area with a -  block, or we could create a separate "container" for every data type we need just to avoid casting. While both ways could work (and worked for many years), it is unnecessary now, because generics offers a much more elegant solution.

To make our "container" class to support any object and avoid casting, we replace every previous type with some new name, in this case, and add  mark immediately after the class name to indicate that this  type is generic/any type.


 * Note: You can choose any name and use more than one generic type for class, i.e.

Not a big difference, which results in simple and safe usage:

Generics ensures that you specify the type for a "container" once, avoiding previously mentioned problems and autoboxing for s.

While this example is far from practical, it does illustrate some situations where generics are useful:
 * You need to keep objects of a single type in a class
 * You do not need to modify objects
 * You need to manipulate objects in some way
 * You wish to store a "value type" (such as, , , or any custom ) in a collection class without incurring the performance penalty of autoboxing every time you manipulate the stored elements.

Generic interfaces
A generic interface accepts one or more type parameters, similar to a generic class:

Generic interfaces are useful when multiple implementations of a particular class are possible. For example, both the class (discussed below) and the  class, both from the  namespace, implement the  interface. has a constructor that creates a new list based on an existing object that implements, and so we can write the following:

Generic methods
Generic methods are very similar to generic classes and interfaces:

This method can be used to search any type of array:

Type constraints
One may specify one or more type constraints in any generic class, interface or method using the keyword. The following example shows all of the possible type constraints:

These type constraints are often necessary to


 * 1) create a new instance of a generic type (the ) constraint
 * 2) use  on a variable of a generic type (the  constraint)
 * 3) use  on a variable of a generic type (the  constraint)