C# Tutorial – 29 – Generics I
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.
Generic methods
In the example below, there is a method that swaps two integer arguments.
static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; }
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 to 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.
static void Swap<T>(ref int a, ref int b) { int temp = a; a = b; b = temp; }
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.
static void Swap<T>(ref T a, ref T b) { T temp = a; a = b; b = temp; }
Calling generic methods
The generic method is now finished. To call it, the desired type argument needs to be specified in angle-brackets before the method arguments.
int a = 0, b = 1; Swap<int>(ref a, ref b);
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.
Swap(ref a, ref b);
Whenever a generic is called for the first time during run-time, a specialized version of the generic will be instantiated that has 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.
Swap<int>(ref a, ref b); // create & call Swap<int> Swap<int>(ref a, ref b); // call Swap<int>
When the generic method is called with a new type, another specialized method will be instantiated.
long c = 0, d = 1; Swap<long>(ref c, ref d); // create & call Swap<long>
Generic type parameters
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.
static void Dummy<T, U>() {} static void Dummy<T>() {}
Default value
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.
static void Reset<T>(ref T a) { a = default(T); }
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

