Action<T>
is contravariant in T, so methods that take Action<T>
also accept Action<U>
, where T derives from U.
However, your IntEvent
class implements the IFace
interface, which means that if the compiler accepted your call to setAction()
, you could call evtCheck()
with an argument that is another IFace
- a derivative, not an IntEvent
, a runtime error and an obvious security violation like .
Required Link Eric Lippert: Covariance and Contravariance
EDIT: from your description, it seems to me that what you really want is differentiation based on the type of the method parameter, i.e. you need a separate action for IntEvent
, another for (hypothetical) DoubleEvent
, etc. If so, you are in fact after a double virtual submit, which is not supported directly in C #, but you can simulate it using the Visitor design template.
source share