To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.
Invert(); Just ruleScreenActive. Invert(); will through the result away.
The NOT operator is used before the variable to be toggled and the result is assigned to the same variable. This will effectively toggle the boolean value.
In fact there is another option to toggle a boolean value, this will work:
_isIt ^= true;
However this is not better than the way you did...
No. That's the optimal way. Congratulations!
Even IF there would be an other way (as you commented - _isIt!!
), I believe that it would be better not to use it because you'd be raising a lot of eyebrows anyway.
Though an answer has been accepted, I believe a more in-depth answer would be nice for future readers to have. As this post has pointed out, the !
or NOT
operator is the most widely accepted, and the optimal solution. In fact, when compared to ^
or XOR
over 10,000 iterations it is roughly 100ms faster. The usage all depends on the circumstance to be honest. Either way, below you will find the three most common methods of toggling a bool
value, and you should choose what you are most comfortable with using, within the standards you're allowed to use.
if-else
MethodThe easiest way to toggle a bool
value is to write out an if-else
that assigns the new value; this way even a complete beginner to development can understand what is going on. This is actually how toggling is taught in most conceptual classes.
bool b = true;
if (b == true)
b = false;
else
b = true;
Of course the if
can be simplified to if (b)
, but traditionally, you'll write it all the way out for clarity and learning purposes.
As you learn more about development and logical operations, you'll learn about the NOT
operator or !
in C#
. The usage of this operator is the most widely accepted in the industry for multiple reasons. It's simple, it's easy to read, it's easy to understand, and it's fast:
x = !x;
This breaks down to !true = false
and !false = true
.
The XOR
or ^
in C#
is the less-chosen operator for this task, but it is an acceptable operator depending on the situation. As some have pointed out in comments, this operator is especially useful when dealing with longer names or deep-access members. If performance matters, and the operation will be executed multiple times, it's best to go with the NOT
operator, as it is faster. If performance isn't really a concern, then using the XOR
operator is acceptable; though in a professional environment you'll likely need to justify the usage.
b ^= true;
b = b ^ true;
The biggest thing to understand about XOR
prior to using it for a toggle, is understanding how it works logically:
true ^ false = true;
true ^ true = false;
false ^ true = true;
false ^ false = false;
That logic table explains why b ^= false
won't toggle. A XOR
requires one side of the operator to be true
and the other to be false
. Which side it is doesn't matter, but neither side can be the same value or it will evaluate to false
. Hence:
bool b = true;
b ^= false; // true.
b ^= false; // true. So on, and so forth.
bool b = false;
b ^= false; // false.
b ^= false; // false. So on, and so forth.
The answer to the OP's question is simply:
NOT
operator is the cleanest and most efficient solution for toggling.I wish you all the best of luck in your future endeavors, and hopefully this answer helped to shed further light on the topic!
No, I believe that is the idiomatic C# way of toggling a bool
(i.e. assigning it the opposite value).
My opinion: Keep your code as it is. It clearly conveys the intent of the code, which is the most important thing for future readers.
If you are interested in alternative ways of doing it, you could use XOR, but I will argue it is less readable.
_isIt ^= true;
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