我能否像使用 if/then/else 一样有效地使用一个语句切换 boolean 值?
在进入我的应用程序的另一段代码中发现了这一点...
private void whatever()
{
////
//// a bunch of stuff
////
if (SomeBooleanValue)
{
SomeBooleanValue= false;
}
else
{
SomeBooleanValue = true;
}
}
出于好奇,我尝试了这个...
private void whatever_whatever()
{
////
//// the same stuff
////
SomeBooleanValue = !SomeBooleanValue;
}
...并在调试中遍历它,似乎我得到了相同的结果。
是否有充分的理由使用 if/then/else 而不是单行方式?
最佳答案
Is there a good reason to use the if/then/else instead of the single line way
我想不到。对于大多数程序员来说,使用 !
运算符更简洁、更直观。
关于c# - 使用 if/then/else 来翻转 boolean 值或否定是否更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14327984/