Why is class constraint not detected for generic class using an inherited type?

Viewed 519

Phrasing the question was hard, I hope the following code snippet makes things clear:

public class DemoClass<TBase> where TBase : class
{
    public void DemoMethod<T>(T target) where T : TBase
    {
        //The following line causes a design-time error: Type argument 'T' does not satisfy the 'Class' constraint for type parameter 'T'.
        WeakReference<T> demoRef = new WeakReference<T>(target);
    }
}

The WeakReference requires a type, T, that satisfies a class constraint. So far, so good, but...

Why can the compiler not detect that T actually does, because (practically) T : TBase : class?

2 Answers
Related