Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable bool or bool with default value in action?

Perhaps a small question but im curious.

What is favored?

In a controllers action, when passing arguments, when and how should they be used?

public ActionResult Action(bool aBool = false)

or

public ActionResult Action(bool? aNullableBool)

I tend to use defualt-value as its a bit more clear and eassier to check, but am i thinking wrong?

like image 807
Erik Karlsson Avatar asked May 15 '13 09:05

Erik Karlsson


Video Answer


1 Answers

The two are not equivalent. In the first example, the caller must specify true or false, if he does not, false is used.

In the second line, the caller may provide true, or false, or null. You will need to decide how to handle null. That's a third value that you can get. Plus the caller can not omit it. He needs to pass a value.

like image 139
nvoigt Avatar answered Sep 28 '22 06:09

nvoigt