if(!bool) 的真正含义是什么?

What is the real meaning of if(!bool)?

我想知道,到底是什么意思!在给定的表达式中:

bool myBool = AnyMethodThatReturnABoolean();
if(!myBool)
{
    // Do whatever you want
}

我现在已经在使用它,但我希望 myBool 为假,但它更复杂吗?

有!意思是“== false”或“!= true”?

它只是反转 bool 表达式的值。

True 变为 FalseFalse 变为 True

if 块将 运行 仅当括号内的表达式计算为 True.

这是 logical negation operator

The ! operator computes logical negation of its operand. That is, it produces true, if the operand evaluates to false, and false, if the operand evaluates to true.

在你的例子中

if(!myBool)

就像写:

if(myBool == false)

等同于:

if(mybool == false){ 

      //some code
}

只是一种shorthand的写法。