Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 272
MonsterMMORPG Avatar asked Jul 18 '13 16:07

MonsterMMORPG


People also ask

Can we pass multiple optional parameters in C#?

One more ways we can implement optional parameters is using method overloading. Method overloading allows us to create multiple definitions of same method with different parameters. If you're new to method overloading, read Method Overloading In C#.

How do you pass optional parameters while omitting some other optional parameters?

To omit one optional parameter, while providing another in a TypeScript function, pass an undefined value for the optional parameter you want to omit and provide the value for the next, e.g. logArguments(100, undefined, ['a', 'b', 'c']) . Copied!

How do you pass an optional parameter to a function?

To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.

Are optional parameters bad practice?

The thing with optional parameters is, they are BAD because they are unintuitive - meaning they do NOT behave the way you would expect it. Here's why: They break ABI compatibility ! so you can change the default-arguments at one place.


1 Answers

You need to use named parameters, like so:

myfunc(a, c:5); 
like image 193
It'sNotALie. Avatar answered Oct 05 '22 13:10

It'sNotALie.