一旦变量被设置为某个值,就返回该变量。平等超载
我正在重载用于值比较的 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 _valOne
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以利用逻辑 AND (
&&) 运算符,如下所示:
只要任何一个比较的计算结果为 false,整个条件的计算结果就会为 false。如果这三个条件都为 true,则条件返回 true。
You can take advantage of the short-circuiting nature of the logical AND (
&&
) operator, like this: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.
使用 and 条件:
&&默认情况下实现短路。
Use an and condition:
&& implements short-circuiting by default.
另一种方法可能是这样做:
我想这是一个选择问题。我认为 &&一个更具可读性..
Another way could be to do this:
A matter of choice I guess. I'd think the && one is more readable..