It is a constraint on a type parameter, meaning that the type T
given to a generic class or method must inherit from the class Attribute
For example:
public class Foo<T> :
where T : Attribute
{
public string GetTypeId(T attr) { return attr.TypeId.ToString(); }
// ..
}
Foo<DescriptionAttribute> bar; // OK, DescriptionAttribute inherits Attribute
Foo<int> baz; // Compiler error, int does not inherit Attribute
This is useful, because it allows the generic class to do things with objects of type T
with the knowledge that anything that is a T
must also be an Attribute
.
In the example above, it's okay for GetTypeId
to query the TypeId
of attr
because TypeId
is a property of an Attribute
, and because attr
is a T
it must be a type that inherits from Attribute
.
Constraints can also be used on generic methods, with the same effect:
public static void GetTypeId<T>(T attr) where T : Attribute
{
return attr.TypeId.ToString();
}
There are other constraints you can place on a type; from MSDN:
where T: struct
The type argument must be a value
type. Any value type except Nullable
can be specified.
where T : class
The type argument must be a reference
type; this applies also to any class,
interface, delegate, or array type.
where T : new()
The type argument must have a public
parameterless constructor. When used
together with other constraints, the
new() constraint must be specified
last.
where T : <base class name>
The type argument must be or derive
from the specified base class.
where T : <interface name>
The type argument must be or implement
the specified interface. Multiple
interface constraints can be
specified. The constraining interface
can also be generic.
where T : U
The type argument supplied for T must
be or derive from the argument
supplied for U. This is called a naked
type constraint.
This is a constraint on the generic parameter of your class, meaning that any type that is passed as the generic type must have a parameterless constructor.
So,
public class C : B
{
public C() {}
}
would be a valid type. You could create a new instance of A<C>
.
However,
public class D : B
{
public D(int something) {}
}
would not satisfy the constraint, and you would not be allowed to create a new instance of A<D>
. If you also added a parameterless constructor to D, then it would again be valid.
Best Answer
That is a constraint on the generic parameter
T
. It must be aclass
(reference type) and must have a public parameter-less default constructor.That means
T
can't be anint
,float
,double
,DateTime
or any otherstruct
(value type).It could be a
string
, or any other custom reference type, as long as it has a default or parameter-less constructor.