Can I switch a boolean with one statement as effectively as with an if/then/else ?
Found this in another piece of code that is going into my app…
private void whatever()
{
////
//// a bunch of stuff
////
if (SomeBooleanValue)
{
SomeBooleanValue= false;
}
else
{
SomeBooleanValue = true;
}
}
Out of curiosity, I tried this…
private void whatever_whatever()
{
////
//// the same stuff
////
SomeBooleanValue = !SomeBooleanValue;
}
…and walked through it in debug, and it appears that I get the same result.
Is there a good reason to use the if/then/else instead of the single line way ?
The 1-line way is perfectly fine, and the only reason why you’d use the
if/ elsestructure is if you were doing other things aside from just toggling the boolean.