"T: class" will force the generic type specified as a class, not a value. For example, we can create an ObjectList class that requires the generic type to be specified as a class, not a value:
class ObjectList<T> where T : class { ... } class SomeObject { ... } ObjectList<int> invalidList = new ObjectList<int>();
This forces an invariant of your generic type T, which might otherwise be invalid. "T: struct" will work the same. Note that you can also use this construct to enforce not only that the type T is a class, but also that it matches the interface. The sample code I used also has
class MyList<T> where T : class, IEntity { ... }
which makes T be a class AND is also IEntity.
Joe B.
source share