10

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?

3
  • Think whether your column in DB is null or not. If yes, use nullable Commented May 15, 2013 at 9:29
  • Here's a third way to throw in there: public ActionResult Action([DefaultValue(false)]bool aBool) :) Commented May 15, 2013 at 9:32
  • @Silvermind Yep :) haha Commented May 15, 2013 at 9:33

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Perhaps I am mistaken, but if you omit the value in the second example it would be null.
@Silvermind That's incorrect, the type is nullable not optional, subtle difference, but with nullable you need to specify something, even if it's just null.
@Silvermind, if you want to do this you will must write : public ActionResult Action(bool? aNullableBool = null)
@mattytommo Thanks, I stand corrected. I was confused with ?aNullableBool=&etc so without a value in the querystring.
wording is a bit odd... saying the user must specify... but then if he does not, false is used ? Contradiction
|
4

The .HasValue property of nullable variables can be quite handy sometimes. Having a nullable bool is like having a bool in a bool. Take Chuong Les example with the database, when you read a cell you may want to check that the cell actually holds a value before continuing (and ending up with an error further down the line).

Instead of having to make sure that isn't the case before reading the cell you can use a nullable variable and use if (aNullableBool.HasValue) to make sure you have a value before continuing.

That beeing said, unless you run the risk of the variable getting the value null you should use a default value.

Comments

0

I think default value in Route configuration will be best.

Comments

0

It depends of the role of your bool.

If you want to have a default value or not (default behavior + just one other behavior), so just 2 behavior of this method.

If you want to have a third behavior as a default behavior, you will need to use a nullable bool.

So think about the needs of your method and choose accordingly.

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.