Generics refer to the use of type parameters,
which provide a way to design code templates that can operate with different data types.
Specifically, it is possible to create generic methods, classes, interfaces, delegates, and events.
In the following example, there is a method that swaps two integer-arguments.
To make this into a generic method that can work with any data type,
a type parameter first needs to be added after the method's name, enclosed between angle-brackets.
The naming convention for type parameters is that they should start with a capital T,
and then have each word that describes the parameter initially capitalized.
In cases such as this however, where a descriptive name would not add much value,
it is common to simply name the parameter with a capital T.
The type parameter can now be used as any other type inside the method,
and so the second thing that needs to be done to complete the generic method,
is to replace the data type that will be made generic with the type parameter.
The generic method is now finished.
To call it the desired type argument needs to be specified in angle-brackets before the method arguments.
In this case, the generic method may also be called as if it was a regular method, without specifying the type argument.
This is because the compiler can automatically determine the type, since the generic method's parameters use the type parameter.
However, if this was not the case, or to use another type argument than the one the compiler would select,
the type argument would then need to be explicitly specified.
Whenever a generic is called for the first time, during run-time, a specialized version of the generic will be instantiated.
This method will have every occurrence of the type parameter substituted with the specified type argument.
It is this generated method that will be called, and not the generic method itself.
Calling the generic method again with the same type argument will reuse this instantiated method.
When the generic method is called with a new type, another specialized method will be instantiated.
A generic can be defined to accept more than one type parameter, just by adding more of them between the angle brackets.
Generic methods can also be overloaded based on the number of type parameters that they define.
When using generics, one issue that may arise is how to assign a default value to a type parameter,
since this value depends on the type.
The solution is to use the default keyword, followed by the type parameter enclosed in parentheses.
This expression will return the default value no matter which type parameter is used.
Generic classes allow class members to use type parameters.
They are defined in the same way as generic methods, by adding a type parameter after the class name.
To instantiate an object from the generic class the standard notation is used,
but with the type argument specified after both class names.
Note that in contrast to generic methods,
a generic class must always be instantiated with the type argument explicitly specified.
Inheritance works slightly differently with generic classes.
A generic class can first of all inherit from a non-generic class, also called a concrete class.
Second, it can inherit from another generic class that has its type argument specified,
a so called closed constructed base class.
Finally, it can inherit from an open constructed base class,
which is a generic class that has its type argument left unspecified.
A generic class that inherits from an open constructed base class must define all of the base class's type arguments,
even if the derived generic class does not need them.
This is because only the child class's type arguments can be sent along when the child class is instantiated.
This also means that a non-generic class can only inherit from a closed constructed base class,
and not from an open constructed base class,
because a non-generic class cannot specify any type arguments when it is instantiated.
Interfaces that are declared with type parameters become generic interfaces.
Generic interfaces have the same two purposes as regular interfaces.
They are either created to expose members of a class that will be used by other classes,
or to force a class to implement a specific functionality.
When a generic interface is implemented by a non-generic class, the type argument must be specified.
The generic interface can also be implemented by a generic class, in which case the type argument can be left unspecified.
A delegate can be defined with type parameters.
As an example, the generic delegate here uses its type parameter to specify the referable method's parameter.
From this delegate type a delegate object can be created that can refer to any
void method that takes a single argument, regardless of its type.
Generic delegates can be used to define generic events.
For example, instead of using the typical design pattern, where the sender of the event is of the Object type,
a type parameter can allow the sender's actual type to be specified.
This will make the argument strongly-typed,
which allows the compiler to enforce that the correct type is used for that argument.
In general, using the Object type as a universal container should be avoided.
The reason why Object containers, such as the ArrayList, exist in the .NET class library,
is because generics were not introduced until C# 2.0.
When compared with the Object type, generics not only ensure type safety at compile-time,
but they also remove the performance overhead associated with boxing and unboxing value types into an Object container.
When defining a generic class or method, compile-time enforced restrictions can be applied
on the kinds of type arguments that may be used when the class or method is instantiated.
These restrictions are called constraints, and they are specified using the "where" keyword.
All in all there are six kinds of constraints.
First, the type parameter can be restricted to value types by using the struct keyword.
Second, the parameter can be constrained to reference types by using the class keyword.
Third, the constraint can be a class name.
This will restrict the type to either that class or one of its derived classes.
Fourth, the type can be constrained to either be or derive from another type parameter.
The fifth constraint is to specify an interface.
This will restrict the type parameter to only those types that implement the specified interface,
or that is of the interface type itself.
Finally, the type argument can be constrained to only those types that have a public parameterless constructor.
Multiple constraints can be applied to a type parameter, by specifying them in a comma separated list.
Furthermore, to constrain more than one type parameter, additional where clauses can be added.
Keep in mind that if either the class or the struct constraint is used, it must appear first in the list.
Moreover, if the parameterless constructor constraint is used, it must be the last one in the list.
Aside from restricting the use of a generic method or class to only certain parameter types,
another reason for applying constraints is to increase the number of allowed operations
and method calls supported by the constraining type.
An unconstrained type may only use the System.Object methods.
However, by applying a base class constraint, the accessible members of that base class also become available.
To give another example, the base class is now made generic,
and its type parameter is constrained to classes with a parameterless constructor.
Through this constraint it becomes possible to create a new instance of that type.
Note that if a class has a constraint on its type parameter,
and a child of that class has a type parameter which is constrained by the base class,
that constraint must also be applied to the child class's type parameter.