Search code examples
c#functionoptional-parametersoptional-argumentsoptional-variables

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


Solution

  • You need to use named parameters, like so:

    myfunc(a, c:5);