Is it possible to use the return value of a function instead of a specific value as optional parameter in a function? For example instead of:
public void ExampleMethod(int a, int b, int c=10)
{
}
I want something like
private int ChangeC(int a, int b)
{
return a+b;
}
public void ExampleMethod(int a, int b, int c=ChangeC(a,b))
{
}
No this is not possible. For a parameter to be optional the value must be a compile time constant. You can however overload the method like so:
private int ChangeC(int a, int b)
{
return a + b;
}
public void ExampleMethod(int a, int b, int c) {}
public void ExampleMethod(int a, int b)
{
ExampleMethod(a, b, ChangeC(a, b));
}
This way you don't have to deal with nullable value types
One of the ways:
private int ChangeC(int a, int b)
{
return a+b;
}
public void ExampleMethod(int a, int b, int? c=null)
{
c = c ?? ChangeC(a,b);
}
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