user9301520
user9301520

Reputation:

Override 2nd optional parameter while leaving the 1st optional parameter alone

I'm still pretty new to C#, so I'm looking for a way to only override certain optional parameters while leaving others alone. I would have something like this:

Private void DoSomething(string Var1, int Var2 = 0, string Var3 = "TEST"){//Do something}

Then when I try to do this:

DoSomething("my variable",,"OK");

I always get an error saying parameter missing. Is there a way to override optional parameters without overriding optional parameters before them?

Thanks for the help.

Upvotes: 0

Views: 170

Answers (2)

Darren Clark
Darren Clark

Reputation: 793

You use a named argument:

DoSomething("My variable",Var3:"OK");

Upvotes: 2

clarkitect
clarkitect

Reputation: 1730

Short answer: yes

Longer answer: use the parameter names and, preferably, use clear names for those parameters.

Example:

DoSomething(Var1: "my variable", Var3:"OK");

Upvotes: 1

Related Questions