Generic Constraints
Sometimes you want to limit the types that can be used in a generic. You can do that using constraints:
where T : class
– T must be a reference type.
where T : struct
– T must be a value type.
where T : new()
– T must have a parameterless constructor.
where T : SomeBaseClass
– T must inherit from SomeBaseClass
.
where T : SomeInterface
– T must implement SomeInterface
.