Should I use `!IsGood` or `IsGood == false`?

后端 未结 30 2401
礼貌的吻别
礼貌的吻别 2020-12-02 19:49

I keep seeing code that does checks like this

if (IsGood == false)
{
   DoSomething();
}

or this

if (IsGood == true)
{
   D         


        
30条回答
  •  渐次进展
    2020-12-02 20:49

    In many languages, the difference is that in one case, you are having the compiler/interpreter dictate the meaning of true or false, while in the other case, it is being defined by the code. C is a good example of this.

    if (something) ...
    

    In the above example, "something" is compared to the compiler's definition of "true." Usually this means "not zero."

    if (something == true) ...
    

    In the above example, "something" is compared to "true." Both the type of "true" (and therefor the comparability) and the value of "true" may or may not be defined by the language and/or the compiler/interpreter.

    Often the two are not the same.

提交回复
热议问题