This question already has an answer here:
Is there a chance to make this code work? Of course I can make second definition of Foo, but I think it'd be a little non-elegant ;)
delegate int Del(int x);
static int Foo(int a, int b = 123)
{
return a+b;
}
static void Main()
{
Del d = Foo;
}
Optional parameters do not change the signature of the method. They simply declare default values for the parameters. This information is used by the compiler to supply values when you omit them in your code. The compiled code will still pass arguments for all parameters.
In your case, the method
Foo
is still declared as taking twoint
arguments as input. There is no version ofFoo
that can be invoked with one parameter only (remember, the compiler fills in the blanks for you there). Any delegates used for invoking methods with optional parameters, need to explicitly include all parameters in order to match the signature.Optional parameters do not change the signature of the method, which is critical to delegates. It only appears to change the signature from the perspective of the caller. What you are trying to achieve cannot be done using the method you have attempted to use.
See this question: Optional parameters on delegates doesn't work properly
Your delegate asks for exactly one parameter, while your
Foo()
method asks for at most two parameters (with the compiler providing default values for unspecified call arguments). Thus the method signatures are different, so you can't associate them this way.To make it work, you need to either overload your
Foo()
method (like you said), or declare your delegate with the optional parameter:By the way, bear in mind that if you declare different default values in your delegate and the implementing method, the default value defined by the delegate type is used.
That is, this code prints
457
instead of124
becaused is Del
: