News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

C# Tutorial – 29 – Generics

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 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); 
}

Generic classes

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.

class Point<T>
{
  public T x, y;
}

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.

Point<short> p = new Point<short>();

Generic class inheritance

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.

class BaseConcrete  {}
class BaseGeneric<T>{}
 
class Gen1<T> : BaseConcrete    {} // concrete
class Gen2<T> : BaseGeneric<int>{} // closed constructed
class Gen3<T> : BaseGeneric<T>  {} // open constructed

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.

class BaseMultiple<T, U, V> {}
class Gen4<T, U> : BaseMultiple<T, U, int> {}

This also means that a non-generic class can only inherit from a closed constructed base class, and not from an open one, because a non-generic class cannot specify any type arguments when it is instantiated.

class Con1 : BaseGeneric<int> {} // ok
class Con2 : BaseGeneric<T> {}   // error

Generic interfaces

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.

// Generic functionality interface
interface IGenericCollection<T> 
{ 
  void Store(T t); 
}
 
// Non-generic class implementing generic interface
class Box : IGenericCollection<int> 
{
  public int myBox;
  public void Store(int i) { myBox = i; }
}
 
// Generic class implementing generic interface
class GenericBox<T> : IGenericCollection<T>
{
  public T myBox;
  public void Store(T t) { myBox = t; }
}

Generic delegates

A delegate can be defined with type parameters. As an example, the generic delegate below 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.

class MyClass
{
  public delegate void MyDelegate<T>(T arg);
 
  public void Print(string s) 
  { 
    System.Console.Write(s); 
  }
 
  static void Main()
  {
    MyDelegate<string> d = Print;
  }
}

Generic events

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 senders 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.

class MyClass
{
  delegate void MyDelegate<T, U>(T sender, U eventArgs);
  event MyDelegate<MyClass, System.EventArgs> myEvent;
}

Generics and Object

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.

// Object container class
class MyBox { public object o; }
 
// Generic container class
class MyBox<T> { public T o; }
 
class MyClass
{
  static void Main()
  {
    // .NET object container
    System.Collections.ArrayList a;  
 
    // .NET generic container (preferred)
    System.Collections.Generic.List<int> b;
  }
}

Constraints

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 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.

class C<T> where T : struct {} // value type

Second, the parameter can be constrained to reference types by using the class keyword.

class D<T> where T : class {} // reference type

Third, the constraint can be a class name. This will restrict the type to either that class or one of its derived classes.

class B {}
class E<T> where T : B {} // be/derive from base class

Fourth, the type can be constrained to either be or derive from another type parameter.

class F<T, U> where T : U {} // be/derive from U

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.

interface I {}
class G<T> where T : I {} // be/implement interface

Finally, the type argument can be constrained to only those types that have a public parameterless constructor.

class H<T> where T : new() {} // no parameter constructor

Multiple constraints

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. Note 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.

class J<T, U>
  where T : class, I
  where U : I, new() {}

Why to use constraints

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.

class Person
{
  public string name;
}
 
class PersonNameBox<T> where T : Person
{
  public string box;
 
  public void StorePersonName(T a)
  {
    box = a.name;
  }
}

Another example below uses the parameterless constructor constraint. This constraint enables new objects of the type parameter to be instantiated.

class MyClass<T> where T : new() {}

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.

class MyChild<T> : MyClass<T> where T : MyClass<T>, new() {}