public static void ContravariantGeneric() { IContravariantGeneric<MiddleClass> genericMiddle = new ContravariantGeneric<MiddleClass>(); genericMiddle.Method(new MiddleClass()); // This will produce compile-time error: // Cannot implicitly convert type 'IContravariantGeneric<MiddleClass>' to 'IContravariantGeneric<BaseClass>'. // An explicit conversion exists (are you missing a cast?) //// IContravariantGeneric<BaseClass> genericBase = genericMiddle; // This is OK: IContravariantGeneric<LastClass> genericLast = genericMiddle; }
public static void ContravariantGeneric() { IContravariantGeneric <MiddleClass> genericMiddle = new ContravariantGeneric <MiddleClass>(); genericMiddle.Method(new MiddleClass()); // This will produce compile-time error: // Cannot implicitly convert type 'IContravariantGeneric<MiddleClass>' to 'IContravariantGeneric<BaseClass>'. // An explicit conversion exists (are you missing a cast?) //// IContravariantGeneric<BaseClass> genericBase = genericMiddle; // This is OK here: IContravariantGeneric <LastClass> genericLast = genericMiddle; genericLast.Method(new LastClass()); }