One of the great things about generics is that their type parameters can be limited. This might seem counterintuitive to what we've learned about generics so far, but just because a class can contain any type, doesn't mean it should necessarily be allowed to.
To constrain a generic type parameter, we need a new keyword and a syntax we haven't seen before:
public class SomeGenericCollection<T> where T: ConstraintType {}
The where keyword defines the rules that T must pass before it can be used as a generic type parameter. It essentially says SomeGenericClass can take in any T type as long as it conforms to the constraining type. The constraining rules aren't anything mystical or scary; they're concepts we've already covered:
- Adding the class keyword would constrain T to types that are classes.
- Adding the struct keyword would constrain T to types that are structs.
- Adding an interface, such...