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;
}
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In C#, the using keyword has two purposes: The first is the using directive, which is used to import namespaces at the top of a code file. The second is the using statement. C# 8 using statements ensure that classes that implement the IDisposable interface call their dispose method.
There is at least one C compiler for almost every existent architecture. And nowadays, because of highly optimized binaries generated by modern compilers, it's not an easy task to improve on their output with hand written assembly.
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 two int
arguments as input. There is no version of Foo
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.
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:
delegate int Del(int x, int y = 123);
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 of 124
because d is Del
:
delegate int Del(int x, int y = 456);
static int Foo(int a, int b = 123)
{
return a+b;
}
static void Main()
{
Del d = Foo;
Console.WriteLine(d(1));
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With