I want to achieve following scenario:
Method:
public void Process(Request request, string statusCode=request.statusCode, string statusVal=request.statusVal)
{
...
}
Obviously above construct does not work in C#.
So in this case method process takes 3 parameters where calling parameter may or may not pass statusCode and/or statusVal in arguments.
When the values are not passed it should take the values from request object as default value. In short can the default value of optional parameters be dynamic or based on another compulsory parameter.
Here is how you can use optional parameters as described:
public void Process(Request request, string statusCode = null, string statusVal= null)
{
statusCode = statusCode ?? request.statusCode;
statusVal = statusVal ?? request.statusVal;
...
}
Now you can call it any of these ways:
Process(request);
Process(request, "code");
Process(request, "code", "val");
Process(request, statusCode: "code");
Process(request, statusVal: "val");
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