A
Aryeh Holzer
Let me start with a quote from the C# Programmers Reference (where I
learned the cool word "covariance"):
"When a delegate method has a return type that is more derived than the
delegate signature, it is said to be covariant. Because the method's
return type is more specific than the delegate signature's return type,
it can be implicitly converted. The method is therefore acceptable for
use as a delegate."
It then goes on to give an archetypal example using Mammals and Dogs,
which is all well and good because both of those are reference types.
However, I'm working on a piece of code where I wanted to use a value
types for the return type from the delegate, and it just won't work.
Here's some sample code (adapted from the C# Programmers Reference
example actually):
public delegate object MyHandlerMethod();
public static string MyFirstHandler()
{
return null;
}
public static int MySecondHandler()
{
return 1;
}
static void Main(string[] args)
{
MyHandlerMethod handler_1 = new MyHandlerMethod(MyFirstHandler);
// Covariance should allows this delegate, but it doesn't
// error CS0407: 'int MyFirstHandler()' has the wrong return type
MyHandlerMethod handler_2 = new MyHandlerMethod(MySecondHandler);
}
I was a bit surprised, since supposedly all value types (such as int)
derive from the same base, System.Object. Furthermore, even specifying
ValueType as the return from the delegate (and limiting its use to value
types such as MySecondHandler, of course) doesn't change anything. Is
there anyway to achieve covariance using ValueType's, or am I missing
something real obvious here?
-Aryeh Holzer
learned the cool word "covariance"):
"When a delegate method has a return type that is more derived than the
delegate signature, it is said to be covariant. Because the method's
return type is more specific than the delegate signature's return type,
it can be implicitly converted. The method is therefore acceptable for
use as a delegate."
It then goes on to give an archetypal example using Mammals and Dogs,
which is all well and good because both of those are reference types.
However, I'm working on a piece of code where I wanted to use a value
types for the return type from the delegate, and it just won't work.
Here's some sample code (adapted from the C# Programmers Reference
example actually):
public delegate object MyHandlerMethod();
public static string MyFirstHandler()
{
return null;
}
public static int MySecondHandler()
{
return 1;
}
static void Main(string[] args)
{
MyHandlerMethod handler_1 = new MyHandlerMethod(MyFirstHandler);
// Covariance should allows this delegate, but it doesn't
// error CS0407: 'int MyFirstHandler()' has the wrong return type
MyHandlerMethod handler_2 = new MyHandlerMethod(MySecondHandler);
}
I was a bit surprised, since supposedly all value types (such as int)
derive from the same base, System.Object. Furthermore, even specifying
ValueType as the return from the delegate (and limiting its use to value
types such as MySecondHandler, of course) doesn't change anything. Is
there anyway to achieve covariance using ValueType's, or am I missing
something real obvious here?
-Aryeh Holzer