I have a method:
public void MyMethod(string myParam1,string myParam2="")
{
myParam2 = (myParam2 == "")?myParam1:myParam2;
}
Is there any way to do this something like:
public void MyMethod(string myParam1,string myParam2 = (myParam2 == "")?myParam1:myParam2)
No.
The default value of parameters needs to be known at compile time. The first snippet you provided is the correct way to do this. Or as has been pointed out by other answers, provide an overload method that only accepts a single parameter.
In order to perform what you want you'll need to use an overload instead of an optional parameter.
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