Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameters - specify one out of several

Tags:

c#

I have a method which accepts three optional parameters.

public int DoStuff(int? count = null, bool? isValid = null, string name = "default")
{
    //Do Something
}

My question is, if I call this method and pass a single method argument:

var isValid = true;
DoStuff(isValid);

I get the the following error:

Argument 1: cannot convert from 'bool' to 'int?'

Is it possible to pass a single method argument, and specify which parameter I wish to specify?

like image 398
CinnamonBun Avatar asked Nov 29 '22 11:11

CinnamonBun


2 Answers

Since the first parameter is count, it is expecting an int? not a bool.

You can provide named parameters like it describes here.

DoStuff(isValid: true);
like image 184
davisoa Avatar answered Dec 10 '22 06:12

davisoa


Yes, it is possible.

DoStuff(isValid: true);
like image 37
Servy Avatar answered Dec 10 '22 06:12

Servy