I have a nullable bool. What is a quick way to invert it. In otherwords if value is TRUE make it FALSE, otherwise make it TRUE.
To clarify (from the comments):
Expected behavior is: if the nullable bool has a value, then invert, otherwise should return null.
myBool = !myBool;
Edit: OK, based on a refined understanding of the question (i.e. myBool
says null if it was null), the above is the simplest answer.
Edit, drblaise is right, !
works just fine
bool? a = null;
bool? b = false;
bool? c = true;
a = !a;
b = !b;
c = !c;
Assert.AreEqual(a, null);
Assert.AreEqual(b, true);
Assert.AreEqual(c, false);
here is the truth table, I know, it's boring but I wanted to see how SO handled "tables"
value !value
|---------|-----------|
| null | null |
|---------|-----------|
| false | true |
|---------|-----------|
| true | false |
|---------|-----------|
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With