一旦变量被设置为某个值,就返回该变量。平等超载

发布于 2024-10-26 20:24:31 字数 1654 浏览 1 评论 0原文

我正在重载用于值比较的 Equality 方法,并且想知道是否有一种干净的方法可以在其中一个值比较返回 false 时立即返回 false。例如,基本思想如下:

public class MyClass
{
    private int _valOne;
    private int _valTwo;
    private int _valThree;

    public MyClass(int valOne, int valTwo, int valThree)
    {
        _valOne = valOne;
        _valTwo = valTwo;
        _valThree = valThree;
    }

    public override bool Equals(object obj)
    {
        // If the object is null return false
        if (obj == null)
        {
            return false;
        }

        // If the object is not of MyClass type return false
        MyClass myClass = obj as MyClass;
        if (myClass == null)
        {
            return false;
        }

        // Now compare all the field values
        bool areEqual = false;
        areEqual = (this._valOne == myClass._valOne);
        areEqual = (this._valTwo == myClass._valTwo);
        areEqual = (this._valThree == myClass._valThree);

        return areEqual;
    }
}

假设 _valOne 不相等。最有效的比较方法是在知道两个值不相等时立即返回 false。如下所示...

        // Now compare all the field values
        bool areEqual = false;

        areEqual = (this._valOne == myClass._valOne);
        if (!areEqual)
        {
            return false;
        }

        areEqual = (this._valTwo == myClass._valTwo);
        if (!areEqual)
        {
            return false;
        }

        areEqual = (this._valThree == myClass._valThree);

        return areEqual;

所以现在在比较 _valOne 后,不再进行值比较。这看起来非常重复、笨拙,而且(最重要的是)可读性很差。我想知道是否有任何方法可以在不使用 && 的情况下以干净的方式将这段代码简化为相同的效果。操作员。

I am overloading the Equality method for value comparison and was wondering if there is a clean way to return false as soon as one of the value comparisons returns false. For example, here is the basic idea:

public class MyClass
{
    private int _valOne;
    private int _valTwo;
    private int _valThree;

    public MyClass(int valOne, int valTwo, int valThree)
    {
        _valOne = valOne;
        _valTwo = valTwo;
        _valThree = valThree;
    }

    public override bool Equals(object obj)
    {
        // If the object is null return false
        if (obj == null)
        {
            return false;
        }

        // If the object is not of MyClass type return false
        MyClass myClass = obj as MyClass;
        if (myClass == null)
        {
            return false;
        }

        // Now compare all the field values
        bool areEqual = false;
        areEqual = (this._valOne == myClass._valOne);
        areEqual = (this._valTwo == myClass._valTwo);
        areEqual = (this._valThree == myClass._valThree);

        return areEqual;
    }
}

Say the _valOne's do not equal. The most efficient way to compare is to return false as soon as it is known two values are not equal. Something like the following...

        // Now compare all the field values
        bool areEqual = false;

        areEqual = (this._valOne == myClass._valOne);
        if (!areEqual)
        {
            return false;
        }

        areEqual = (this._valTwo == myClass._valTwo);
        if (!areEqual)
        {
            return false;
        }

        areEqual = (this._valThree == myClass._valThree);

        return areEqual;

So now after the comparison of the _valOnes no more value comparisons are made. This seems very repetitive, clunky, and (most importantly) horrible for readability. I want to know if there is any way this code can be reduced to the same effect in a clean way without the use of the && operator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-11-02 20:24:31

您可以利用逻辑 AND (&&) 运算符,如下所示:

return this._valOne == myClass._valOne
    && this._valTwo == myClass._valTwo
    && this._valThree == myClass._valThree;

只要任何一个比较的计算结果为 false,整个条件的计算结果就会为 false。如果这三个条件都为 true,则条件返回 true。

You can take advantage of the short-circuiting nature of the logical AND (&&) operator, like this:

return this._valOne == myClass._valOne
    && this._valTwo == myClass._valTwo
    && this._valThree == myClass._valThree;

As soon as any one of the comparisons evaluates to false, the whole condition evaluates to false. If all three are true, the condition returns true.

青柠芒果 2024-11-02 20:24:31

使用 and 条件:

areEqual = (this._valOne == myClass._valOne) 
    && (this._valTwo == myClass._valTwo) 
    && (this._valThree == myClass._valThree);

&&默认情况下实现短路。

Use an and condition:

areEqual = (this._valOne == myClass._valOne) 
    && (this._valTwo == myClass._valTwo) 
    && (this._valThree == myClass._valThree);

&& implements short-circuiting by default.

亽野灬性zι浪 2024-11-02 20:24:31

另一种方法可能是这样做:

    if (this._valOne != myClass._valOne)
         return false;
    if (this._valTwo != myClass._valTwo)
         return false;
    if (this._valThree != myClass._valThree)
         return false;
    return true;

我想这是一个选择问题。我认为 &&一个更具可读性..

Another way could be to do this:

    if (this._valOne != myClass._valOne)
         return false;
    if (this._valTwo != myClass._valTwo)
         return false;
    if (this._valThree != myClass._valThree)
         return false;
    return true;

A matter of choice I guess. I'd think the && one is more readable..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文