隐式(bool)和==运算符覆盖 - 正确处理if语句

Ran*_*dom 13 c# overriding operators implicit

我有一个自定义类,同时实现了==和implicit布尔运算符.

这是处理所有可能的if == /!=语句并获得预期结果的正确方法吗?像这样:

public class Foo
{
    public bool Result { get; set; }

    public static bool operator ==(bool @bool, Foo foo)
    {
        return Equals(foo, @bool);
    }
    public static bool operator !=(bool @bool, Foo foo)
    {
        return NotEquals(foo, @bool);
    }
    public static bool operator ==(Foo foo, bool @bool)
    {
        return Equals(foo, @bool);
    }
    public static bool operator !=(Foo foo, bool @bool)
    {
        return NotEquals(foo, @bool);
    }
    public static bool operator ==(Foo foo, Foo fooB)
    {
        return Equals(foo, fooB);
    }
    public static bool operator !=(Foo foo, Foo fooB)
    {
        return NotEquals(foo, fooB);
    }
    public static implicit operator bool(Foo foo)
    {
        try { return foo.Result; }
        catch { return false; }
    }

    private static bool Equals(Foo foo, Foo fooB)
    {
        if (object.Equals(foo, null))
        {
            if (object.Equals(fooB, null))
                return true;

            return false;
        }

        if (object.Equals(fooB, null))
            return false;

        return foo.Result == fooB.Result;
    }
    private static bool NotEquals(Foo foo, Foo fooB)
    {
        if (object.Equals(foo, null))
        {
            if (object.Equals(fooB, null))
                return false;

            return true;
        }

        if (object.Equals(fooB, null))
            return true;

        return fooB.Result != foo.Result;
    }
    private static bool Equals(Foo foo, bool @bool)
    {
        if (object.Equals(foo, null))
            return true;

        return @bool == foo.Result;
    }
    private static bool NotEquals(Foo foo, bool @bool)
    {
        if (object.Equals(foo, null))
            return false;

        return @bool != foo.Result;
    }

}
Run Code Online (Sandbox Code Playgroud)

我特别想知道它似乎真的需要实现重载

if (new Foo() != true)
Run Code Online (Sandbox Code Playgroud)

和

if (true != new Foo())
Run Code Online (Sandbox Code Playgroud)

Mår*_*röm 15

我觉得你写的代码太多了:-)

以下就足够了:

public class Foo
{
    public bool Result { get; set; }

    public static implicit operator bool(Foo foo)
    {
        return !object.ReferenceEquals(foo, null) && foo.Result;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,编译器会知道如何隐式转换类型的变量Foo成bool.(null并将转换为false).

所以,当你写:

new Foo() == false
Run Code Online (Sandbox Code Playgroud)

编译器将使用隐式类型转换器从中获取bool值Foo,然后使用标准相等运算符bool.

如果我们查看编译器为该表达式生成的IL,我们会发现:

newobj instance void FooBool.Foo::.ctor()               // new Foo()
call bool FooBool.Foo::op_Implicit(class FooBool.Foo)   // implicit operator (Foo => bool)
ldc.i4.0                                                // false
ceq                                                     // equality operator (bool)
Run Code Online (Sandbox Code Playgroud)

这是一个测试:

static void Main(string[] args)
{
    AssertTrue(new Foo() == false);
    AssertTrue(false == new Foo());
    AssertFalse(new Foo() != false);
    AssertFalse(false != new Foo());
    AssertTrue(new Foo { Result = true } == true);
    AssertTrue(true == new Foo { Result = true });
    AssertFalse(new Foo { Result = true } != true);
    AssertFalse(true != new Foo { Result = true });
}

static void AssertTrue(bool value)
{
    Console.WriteLine(value ? "ok" : "not ok");
}

static void AssertFalse(bool value)
{
    Console.WriteLine(value ? "not ok" : "ok");
}
Run Code Online (Sandbox Code Playgroud)

它ok为每个测试打印.因此,如果我理解正确,这个简化的代码应该满足您的需求.

UPDATE

允许等于运算符用于Foo(可以为null)的实例:

public static bool operator ==(Foo a, Foo b)
{
    if (object.ReferenceEquals(a, b))
    {
        return true;
    }
    else if (object.ReferenceEquals(a, null))
    {
        return !b.Result;
    }
    else if (object.ReferenceEquals(b, null))
    {
        return !a.Result;
    }
    else
    {
        return a.Result == b.Result;
    }
}
Run Code Online (Sandbox Code Playgroud)

您还应该实现不等式运算符:

public static bool operator !=(Foo a, Foo b)
{
    return !(a == b);
}
Run Code Online (Sandbox Code Playgroud)

并覆盖GetHashCode+Equals

public override int GetHashCode()
{
    return this.Result ? 1 : 0;
}

public override bool Equals(object obj)
{
    if (object.ReferenceEquals(obj, null))
    {
        return !this.Result;
    }

    Type t = obj.GetType();

    if (t == typeof(Foo))
    {
        return this.Result == ((Foo)obj).Result;
    }
    else if (t == typeof(bool))
    {
        return this.Result == (bool)obj;
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)