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

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

    I would prefer shorter variant. But sometimes == false helps to make your code even shorter:

    For real-life scenario in projects using C# 2.0 I see only one good reason to do this: bool? type. Three-state bool? is useful and it is easy to check one of its possible values this way.

    Actually you can't use (!IsGood) if IsGood is bool?. But writing (IsGood.HasValue && IsGood.Value) is worse than (IsGood == true).

    Play with this sample to get idea:

    bool? value = true; // try false and null too
    
    if (value == true)
    {
        Console.WriteLine("value is true");
    }
    else if (value == false)
    {
        Console.WriteLine("value is false");
    }
    else
    {
        Console.WriteLine("value is null");
    }
    

    There is one more case I've just discovered where if (!IsGood) { ... } is not the same as if (IsGood == false) { ... }. But this one is not realistic ;) Operator overloading may kind of help here :) (and operator true/false that AFAIK is discouraged in C# 2.0 because it is intended purpose is to provide bool?-like behavior for user-defined type and now you can get it with standard type!)

    using System;
    
    namespace BoolHack
    {
        class Program
        {
            public struct CrazyBool
            {
                private readonly bool value;
    
                public CrazyBool(bool value)
                {
                    this.value = value;
                }
    
                // Just to make nice init possible ;)
                public static implicit operator CrazyBool(bool value)
                {
                    return new CrazyBool(value);
                }
    
                public static bool operator==(CrazyBool crazyBool, bool value)
                {
                    return crazyBool.value == value;
                }
    
                public static bool operator!=(CrazyBool crazyBool, bool value)
                {
                    return crazyBool.value != value;
                }
    
                #region Twisted logic!
    
                public static bool operator true(CrazyBool crazyBool)
                {
                    return !crazyBool.value;
                }
    
                public static bool operator false(CrazyBool crazyBool)
                {
                    return crazyBool.value;
                }
    
                #endregion Twisted logic!
            }
    
            static void Main()
            {
                CrazyBool IsGood = false;
    
                if (IsGood)
                {
                    if (IsGood == false)
                    {
                        Console.WriteLine("Now you should understand why those type is called CrazyBool!");
                    }
                }
            }
        }
    }
    

    So... please, use operator overloading with caution :(

提交回复
热议问题