Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In c# what does 'where T : class' mean?

Tags:

syntax

c#

Simply put this is constraining the generic parameter to a class (or more specifically a reference type which could be a class, interface, delegate, or array type).

See this MSDN article for further details.


It's a type constraint on T, specifying that it must be a class.

The where clause can be used to specify other type constraints, e.g.:

where T : struct // T must be a struct
where T : new()  // T must have a default parameterless constructor
where T : IComparable // T must implement the IComparable interface

For more information, check out MSDN's page on the where clause, or generic parameter constraints.


It is a generic type constraint. In this case it means that the generic type T has to be a reference type (class, interface, delegate, or array type).


That restricts T to reference types. You won't be able to put value types (structs and primitive types except string) there.