C# Optional Parameters and Default Value
I wrote a method that takes optional parameter
public void ExampleMethod(int maxValue = 20)
{
...
}
and i will use it as
int param = GetSomeValue();
ExampleMethod(param < 20 ? param : use_your_default_value_as_specified);
if it is less than 20, use param, else use your own default value
implemented in ExampleMethod (in this example it's 20).. How can i tell
that "use_your_default_value_as_specified" to compiler ?
I know i can do this by
int param = GetSomeValue();
ExampleMethod(param);
public void ExampleMethod(int maxValue)
{
if(maxValue > 20)
maxValue = 20;
}
but i want to send the correct value before execution of ExampleMethod
No comments:
Post a Comment