interface<T> where T : class
for example
public interface iSend<T> where T : class
What does the above code mean?
Why to use this?
When to use this?
interface<T> where T : class
for example
public interface iSend<T> where T : class
What does the above code mean?
Why to use this?
When to use this?
Check my full post here : Constrain on custom generic type which talks about different type of generic constrain
it's Reference Type Constrain
Constrain ensure that type argument is Reference Type. i.e Class, Interface, Delegates, Array etc.
interface iSend<T> where T : class
Example
Valid InValid
A<MyClass> A<int>
A<InterfaceME> A<float>
A<float[]>
Note : Always come first when multiple constrain applied.
From the docs:
Constraints on Type Parameters
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. These restrictions are called constraints.
...
where T : class
: The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.
If you use this constraint, then T
has to be a reference type (and no value type).
You do this e.g. to be able to use null
, since reference types can be null
and value types can not.
There are called generic type constraints.
Above code means, you have an generic interface iSend
and it takes only a Reference type as Type Parameter
When you want to limit the Type Parameters for iSend
to be Reference types