From what I remember, "co-variant" means "use a derived type instead of
a base type" and "contra-variant" means "use a base type instead of a
derived type".
That´s exactly the same I know about these things.
A contravariant return type would mean that something declared in the
base type to return "string" could then return an instance of just
"object", and it could *require* a string when the previous parameter
was just object. So this call:
string x = foo.DoSomething (new object());
would fail in both the parameter and the return.
I think the parameter would not fail because, every "string" is an
"object", too. The contravariant return-type would fail, because a
"object" is not always a "string" - a type-safe-failure.
This is just the expected behaviour in a situation where a class is
derived from a base class and a method has been overwritten. In C# 2.0
beta its still not allowed to have co- or contravariance in this
Derived-Class-Situation. But in delegates! Something like this is
possible:
[CoVariance in 2.0 Beta]
---------------------------------------------------------------
class base{}
class derived : base{}
public delegate base DelegateObject();
DelegateObject del = new DelegateObject(Method1);
del += new DelegateObject(Method2);
public static base Method1() {...}
public static derived Method2() {...}
---------------------------------------------------------------
[Contravariance in 2.0 Beta]
--------------------------------------------------------------
class base{}
class derived : base{}
public delegate void DelgateObject(derived aDerived);
DelegateObject del = new DelegateObject(Method1);
del += new DelegateObject(Method2);
del += new DelegateObject(Method3);
public static void Method1(derived aDer){...}
public static void Method2(base aBase){...}
public static void Method3(Object aObject){...}
----------------------------------------------------------------
I don´t understand, why in this special delegate-way, it´s not
possible to have covariant Parameters and contravariant return types?
many Greetings
Matz