I've been looking into that and there is something I can't quite grasp. In few words COvariance is when you need BASE class, but use DERIVED instead, and CONTRAvariance is when you need DERIVED class but use BASE instead, right?
I've been watching this video : https://www.youtube.com/watch?v=3MQDrKbzvqU where the example is given:
//COvariance with object
Mammal m = new Zebra();
//CONTRAvariance with object
Zebra z = new Zebra();
ContravariantMethod(z);
static void ContravariantMethod(Mammal m) { m.Display(); }
in ContravariantMethod base is needed, but I provide derived. So how is that contravariant at all? I come to the conclusion the video is misleeding.
And what about in and out keywords in covariant/contravariant delegates?
While it makes sense not to be allowed to return BASE instead of DERIVED, why it is a problem to return DERIVED instead of BASE, and why the compiler can't implicitly cast it if it's such a problem, as a matter of fact?
And when I try my own contravariance it's expectedly, giving me an error:
mmmmm(new Mammal());
static void mmmmm(Zebra z) { z.Display(); }
ContravariantMethodis not generic, it is not covariant. - Lee