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

后端 未结 30 2490
礼貌的吻别
礼貌的吻别 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:44

    Some people find the explicit check against a known value to be more readable, as you can infer the variable type by reading. I'm agnostic as to whether one is better that the other. They both work. I find that if the variable inherently holds an "inverse" then I seem to gravitate toward checking against a value:

    if(IsGood) DoSomething();
    

    or

    if(IsBad == false) DoSomething();
    

    instead of

    if(!IsBad) DoSomething();
    

    But again, It doen't matter much to me, and I'm sure it ends up as the same IL.

提交回复
热议问题