Example:
public int foo(int x, int optionalY = 1, int optionalZ = 2) { ... }
I'd like to call it like this:
int returnVal = foo(5,,8);
In other words, I want to provide x
and z
, but I want to use the default for Y
, optionalY = 1.
Visual Studio does not like the ,,
Please help.
By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.
C does not support optional parameters.
The thing with optional parameters is, they are BAD because they are unintuitive - meaning they do NOT behave the way you would expect it. Here's why: They break ABI compatibility ! so you can change the default-arguments at one place.
You don't pass option parameters. You pass optional arguments! For more explicit control than that provided by reserving sentinel values, check out boost::optional<>.
If this is C# 4.0, you can use named arguments feature:
foo(x: 5, optionalZ: 8);
See this blog for more information.
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