Yes. Optional parameters must have a constant value specified as default, and come after all other parameters.
public Box(int length,
int width,
int height,
Color color = Color.Brown)
{ ... }
The rule for a constant value means that it's not always possible to use a meaningful default value for a reference type, but that's usually solved by using null as the default value and then checking for null at the beginning of the function.
Don't feel bad. There are actually some minor bumps with default parameters on public types, so using overloads isn't all bad.
For one thing, the default values are constant, and constants don't ever change, so the compiler will inline them. That means if assembly A calls a method with a default parameter in assembly B, it can inline that particular value at compile time. Someone later ships a new version of assembly B with a different value but A is distributed in binary form and not recompiled... it still has the previous value.
2
u/RiPont Oct 31 '19
Yes. Optional parameters must have a constant value specified as default, and come after all other parameters.
The rule for a constant value means that it's not always possible to use a meaningful default value for a reference type, but that's usually solved by using
nullas the default value and then checking for null at the beginning of the function.