# 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 - int) 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 int type in it and no other data type, as in such a case, a InvalidCastException is thrown.
Additionally, if the original data type we have chosen is a value type, such as int, 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 try - catch 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 object type with some new name, in this case T, and add <T> mark immediately after the class name to indicate that this T type is generic/any type.
    Note: You can choose any name and use more than one generic type for class, i.e <genKey, genVal>.
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 structs.
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 int, short, string, or any custom struct) 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 List<T> class (discussed below) and the LinkedList<T> class, both from the System.Collections.Generic namespace, implement the IEnumerable<T> interface. List<T> has a constructor that creates a new list based on an existing object that implements IEnumerable<T>, 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 where keyword. The following example shows all of the possible type constraints:
These type constraints are often necessary to
* create a new instance of a generic type (the new) constraint
* use foreach on a variable of a generic type (the IEnumerable<T> constraint)
* use using on a variable of a generic type (the IDisposable constraint)

## Notes

* ↑ "Generics (C# Programming Guide)". MSDN. Archived from the original on 2013-07-17. Retrieved 2011-08-09.{{cite web}}: CS1 maint: unfit URL (link)