Furkan Gözükara
Furkan Gözükara

Reputation: 23830

Multiple optional parameters calling function

Assume that i have a function like this below It takes 3 parameters and 2 have optional values

private void  myfunc (int a, int b=2, int c=3)
{
  //do some stuff here related to a,b,c
}

now i want to call this function like below how possible ?

myfunc(3,,5)

So i want it to use default parameter b=2

But it is giving error that way.

Here the error message

Argument missing

C# 4.5

Upvotes: 32

Views: 33564

Answers (2)

jltrem
jltrem

Reputation: 12524

call it like this:

myfunc(3, c: 5)

You can read up on named parameters on MSDN. Named parameters can be in any order but must follow positional parameters; i.e., once you use a named parameter you cannot use a positional parameter.

Upvotes: 18

It'sNotALie.
It'sNotALie.

Reputation: 22794

You need to use named parameters, like so:

myfunc(a, c:5);

Upvotes: 68

Related Questions