3

Is there a one-liner to do this?

string to = someNullableBool ? "value1" : "value2";

Currently getting "cannot implicitly convert bool? to bool"... Does the ?? operator come into play here? Or is that only when assigning to a bool?

Basically, if someNullableBool is null or false, that should be treated as false.

JacobIRR
  • 8,545
  • 8
  • 39
  • 68
  • 5
    `someNullableBool == true ? "value1" : "value2";` ? – Scott Oct 16 '17 at 19:40
  • 4
    `(someNullableBool ?? false ) ? "value1" : "value2";` – Igor Oct 16 '17 at 19:40
  • 4
    `someNullableBool.GetValueOrDefault() ? "a" : " b";` – 15ee8f99-57ff-4f92-890c-b56153 Oct 16 '17 at 19:42
  • performance wise, `someNullableBool.GetValueOrDefault() ? "value1" : "value2"` may be preferable (it avoids an `if` / branch), but it is uglier :) – Marc Gravell Oct 16 '17 at 19:42
  • string to = someNullableBool.HasValue ? "value1" : "value2"; – Steve Oct 16 '17 at 19:44
  • 1
    @Steve - that is wrong for the same reason [this answer](https://stackoverflow.com/a/46777944/1260204) is wrong. – Igor Oct 16 '17 at 19:46
  • 1
    Can you explain if you want to get the value of the boolean variable or a string if the variable is null and another string if the variable is not null? – Steve Oct 16 '17 at 19:50
  • `switch(someNullableBool){ case true: to = "value1"; break; default: to = "value2"; break; }` I know you all want to downvote this comment :)))) – M.kazem Akhgary Oct 16 '17 at 19:50
  • 1
    @Steve - good point. The OP should clarify. I think most of us were making the assumption based on the title of the post and assumed the word "value" would mean `false` if it was `null` and treat the rest of the LoC as if it were a regular `bool`. – Igor Oct 16 '17 at 19:52
  • 1
    this one has much better performance :D, `to = (bool)someNullableBool.GetType().GetProperty("HasValue").GetValue(someNullableBool, null) ? (bool)someNullableBool.GetType().GetProperty("Value").GetValue(someNullableBool, null) ? "value1" : "value2" : "value2";` – M.kazem Akhgary Oct 16 '17 at 19:53
  • 2
    @M.kazemAkhgary Most useful content on the entire page. – 15ee8f99-57ff-4f92-890c-b56153 Oct 16 '17 at 19:54

2 Answers2

9

I would use the following it makes sure that you have a value and that the value is true. It is more readable but not the most performant solution.

string to = (someNullableBool.HasValue && someNullableBool.Value) ? "value1" : "value2";
KaffineAddict
  • 436
  • 2
  • 11
4

Depends on how you want someNullableBool to be interpreted if it's null.

If you want null to be treated like either true or false, you can say (someNullableBool ?? valueIfNull) to treat it like one or the other.

If null has to be handled specially, then you'll need to check someNullableBool.HasValue and handle the case where it's null. Once you've done that, the case for true and false can be handled as above.

If the value should never be null, ideally it shouldn't be a nullable in the first place. :P But if that's out of your control, you might want to cast to bool or use the nullable's Value property to make that expectation explicit, and to fail if ever it doesn't hold.

cHao
  • 84,970
  • 20
  • 145
  • 172
  • 2
    `you can simply cast to bool` That will result in it *throwing* if it's null, not being treated as `false` (which may be desirable, for the record, if the value shouldn't actually ever be `null`). See the comments under the question (or just Google) for a million ways to convert a `bool?` to a `bool` where `null` should be treated as `false`. – Servy Oct 16 '17 at 19:50
  • @Servy: Ah...looks like i was looking at the wrong conversion. :P Edited. – cHao Oct 16 '17 at 19:56