In c# what does 'where T : class' mean?
In C# what does where T : class
mean?
Ie.
public IList<T> DoThis<T>() where T : class
Solution 1:
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.
Solution 2:
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.
Solution 3:
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).