class Program
{
public delegate void VoidMethodDelegate();
public delegate int IntMethodDelegate();
static void Main(string[] args)
{
Test(IntMethod);
Test(VoidMethod);
}
static int IntMethod()
{
return 1;
}
static void VoidMethod()
{
}
static void Test(VoidMethodDelegate method)
{
}
static void Test(IntMethodDelegate method)
{
}
}
I am trying to set up an overloaded method that will take two different types of delegates. The delegates differ only by return type -- in both cases they take no input parameters. So in the example above, I would like to be able to call Test() and pass it either a method that returns void, or a method that returns int. When I compile the code above, I get these errors:
error CS0121: The call is ambiguous between the following methods or properties: 'ConsoleApplication1.Program.Test(ConsoleApplication1.Program.VoidMethodDelegate)' and 'ConsoleApplication1.Program.Test(ConsoleApplication1.Program.IntMethodDelegate)'
error CS0407: 'int ConsoleApplication1.Program.IntMethod()' has the wrong return type
error CS0121: The call is ambiguous between the following methods or properties: 'ConsoleApplication1.Program.Test(ConsoleApplication1.Program.VoidMethodDelegate)' and 'ConsoleApplication1.Program.Test(ConsoleApplication1.Program.IntMethodDelegate)'
I know I can work around the errors if I create the delegates with new instead of just passing the method directly, like this:
static void Main(string[] args)
{
Test(new IntMethodDelegate(IntMethod));
Test(new VoidMethodDelegate(VoidMethod));
}
But that syntax is messy, and I'd prefer to be able to pass the method directly, instead of having to wrap it in a call to new. The only solution I've seen is to get rid of the overloaded version of Test() and instead use two different methods, each with a different name.
Can anyone tell me why the compiler is complaining that this is ambiguous? I don't understand whhy the compiler can't decide which of the two overloads to use.