T
Tony Johansson
Hi!
Here is some text from e learning.
"Covariance specifies that the return type of the invoked method must
inherit from the return type of the delegate and contravariance specifies
that the type of each parameter in the delegate must inherit from the type
of the parameter in the invoked method."
What I find funny is the following.
1. If you will use Covariance then the return type of the method must be
derived from the return type of the delegete.
So here we consider the returntype of the delegate as more general then the
returntype of the method. I mean I can see the delegete as a template.
2. If you will use contravariance then we look at the parameter. It would
have been logical if we would have consider the parameter type of the
delegate as more general then the parameter of the method. I mean it would
have followed the same pattern as the covariance. The strange thing is that
it's the other way around. The parameter of the method is consider as more
general then the parameter of the delegete. Very unlogical ??
Here one example of covariance where the return type Dog is derived from
Mammal
class Mammal {}
class Dog : Mammal{}
class Program
{
public delegate Mammal HandlerMethod();
public static Mammal FirstHandler()
{ return null;
public static Dog SecondHandler()
{return null;}
static void Main()
{
HandlerMethod handler1 = new HandlerMethod(FirstHandler);
HandlerMethod handler2 = new HandlerMethod(SecondHandler);
}
}
Here one example of contravaraince where the parameter Dog in the delegete
is derived from Mammal
class Mammal {}
class Dog : Mammal{}
class Program
{
public delegate void HandlerMethod(Dog sampleDog);
public static void FirstHandler(Mammal elephant)
{
}
public static void SecondHandler(Dog sheepDog)
{
}
static void Main(string[] args)
{
HandlerMethod handler1 = FirstHandler;
HandlerMethod handler2 = SecondHandler;
}
}
So my question is why have they not followed the same pattern between
covariance and contravariance so it would be more easy to know where the
type that is more general is located.
//Tony
Here is some text from e learning.
"Covariance specifies that the return type of the invoked method must
inherit from the return type of the delegate and contravariance specifies
that the type of each parameter in the delegate must inherit from the type
of the parameter in the invoked method."
What I find funny is the following.
1. If you will use Covariance then the return type of the method must be
derived from the return type of the delegete.
So here we consider the returntype of the delegate as more general then the
returntype of the method. I mean I can see the delegete as a template.
2. If you will use contravariance then we look at the parameter. It would
have been logical if we would have consider the parameter type of the
delegate as more general then the parameter of the method. I mean it would
have followed the same pattern as the covariance. The strange thing is that
it's the other way around. The parameter of the method is consider as more
general then the parameter of the delegete. Very unlogical ??
Here one example of covariance where the return type Dog is derived from
Mammal
class Mammal {}
class Dog : Mammal{}
class Program
{
public delegate Mammal HandlerMethod();
public static Mammal FirstHandler()
{ return null;
public static Dog SecondHandler()
{return null;}
static void Main()
{
HandlerMethod handler1 = new HandlerMethod(FirstHandler);
HandlerMethod handler2 = new HandlerMethod(SecondHandler);
}
}
Here one example of contravaraince where the parameter Dog in the delegete
is derived from Mammal
class Mammal {}
class Dog : Mammal{}
class Program
{
public delegate void HandlerMethod(Dog sampleDog);
public static void FirstHandler(Mammal elephant)
{
}
public static void SecondHandler(Dog sheepDog)
{
}
static void Main(string[] args)
{
HandlerMethod handler1 = FirstHandler;
HandlerMethod handler2 = SecondHandler;
}
}
So my question is why have they not followed the same pattern between
covariance and contravariance so it would be more easy to know where the
type that is more general is located.
//Tony