32

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

2 Answers 2

69

You need to use named parameters, like so:

myfunc(a, c:5);
Sign up to request clarification or add additional context in comments.

Comments

19

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.