The covariant type is not used as a generic constraint for the interface methods, why? (C#)

95 views Asked by At

The problem:

interface ICovariant<out R>
{
    // The following statement generates a compiler error
    // because you can use only contravariant or invariant types
    // in generic constraints.
    // void DoSomething<T>() where T : R;
}

Why is the generic constraint on methods in this case not allowed.

EDIT: Lol I found the answer, Generic type "T" in the method is actually an argument (or an input) and hence it is not covariant.

0

There are 0 answers