Search code examples
c#type-constraints

Constraints on Type Parameters for object and list of objects


Is there a way to implement constraint type parameters where T is from a base class or if T is a list containing the "base class"?

Something looking like this :

public class SomeClass<T> where T : MyBaseClass, IList<MyBaseClass>

Solution

  • No, you can't create generic constraint to work like OR.

    And just to point that that kind of constraint would have been useless: you would not be able to use neither methods defined in BaseClass nor these from IList (because T could have been the first or the second).