Using a generic (X) delegate in a generic (Y) method where Y extends some base class. Cannot convert to base class

Viewed 93
public abstract class SomeBaseClass {}

public class SomeSpecificClass: SomeBaseClass {
    public int propertyA;
    public string propertyB;
}

public delegate void Callback<T>(T data);


public class Foo {


    void MethodA <T> (Callback<T> theInstance) where T: SomeBaseClass {
        MethodB(theInstance);
    }

    void MethodB(Callback<SomeBaseClass> theInstance) {

    }

    void MethodC() {
        Callback<SomeSpecificClass> cb = (data) => {};
        MethodA(cb);
    }

    void MethodD <T> (T theInstance) where T: SomeBaseClass {
        MethodE(theInstance);
    }

    void MethodE (SomeBaseClass theInstance) {

    }
}

Produces the error in MethodA:

Argument 1: cannot convert from 'Callback<T>' to 'Callback<SomeBaseClass>' [Assembly-CSharp] Callback<T> theInstance

But MethodD works fine passing its instance to MethodE

Why can't I pass the generic Callback<T> instance in MethodA to argument of type Callback<SomeBaseClass> in MethodB when I'm specifiying the constraint that T extends SomeBaseClass

2 Answers
Related