Search code examples
c#c#-4.0optional-parametersnamed-parameters

Optional Specification of some C# Optional Parameters


Suppose you have a method with the following signature:

public void SomeMethod(bool foo = false, bool bar = true) { /* ... */ }

When calling this method, is there a way to specify a value for bar and not foo? It would look something like...

SomeMethod(_, false);

... which would translate to...

SometMethod(false, false);

... at compile-time. Is this possible?


Solution

  • Take a look at named parameters.

        SomeMethod(bar: false);