Search code examples
c#default-valueoptional-parametersoptional-arguments

How can I force the use of a default parameter value in C# (skip optional arguments)?


If I am using a method with multiple optional parameters, how can I call the method without specifying only one of the parameters, letting it use its default value?

Say I have method Foo with multiple optional parameters:

void Foo(string a, int b = 5, bool c = false, float d = 0.5f);

And I want to call this method while giving params b and d a specific value, while not caring about c and giving it its default value:

Foo("Hello World", 6, /*default*/, 1.9f);

What do I write instead of /*default*/ to make this happen?

Note: I found this question elsewhere, closed as duplicate, without an answer, while the answer in the linked duplicate gave no solution to the problem I am proposing. Please consider this question correctly before dismissing it.


Solution

  • Foo("Hello World", b: 6, d: 1.9f);