重载 == (当然还有 != )运算符,我可以绕过 == 来确定对象是否为 null
当我尝试在 C# 中重载运算符 == 和 != 并按照建议覆盖 Equal 时,我发现无法区分普通对象和 null。例如,我定义了一个类Complex。
public static bool operator ==(Complex lhs, Complex rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(Complex lhs, Complex rhs)
{
return !lhs.Equals(rhs);
}
public override bool Equals(object obj)
{
if (obj is Complex)
{
return (((Complex)obj).Real == this.Real &&
((Complex)obj).Imaginary == this.Imaginary);
}
else
{
return false;
}
}
但是当我想使用
if (temp == null)
When temp is true null 时,会发生一些异常。而且我无法使用==来判断lhs是否为null,这会导致无限循环。
这种情况我该怎么办。
我能想到的一种方法是,当我进行检查时,我们可以使用诸如 Class.Equal(object, object) (如果存在)之类的东西来绕过 == 。
解决问题的正常方法是什么?
谢谢。
when I try to overload operator == and != in C#, and override Equal as recommended, I found I have no way to distinguish a normal object and null. For example, I defined a class Complex.
public static bool operator ==(Complex lhs, Complex rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(Complex lhs, Complex rhs)
{
return !lhs.Equals(rhs);
}
public override bool Equals(object obj)
{
if (obj is Complex)
{
return (((Complex)obj).Real == this.Real &&
((Complex)obj).Imaginary == this.Imaginary);
}
else
{
return false;
}
}
But when I want to use
if (temp == null)
When temp is really null, some exception happens. And I can't use == to determine whether the lhs is null, which will cause infinite loop.
What should I do in this situation.
One way I can think of is to us some thing like Class.Equal(object, object) (if it exists) to bypass the == when I do the check.
What is the normal way to solve the problem?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 Equals 覆盖的顶部使用以下内容:
您收到的异常可能是 StackOverflowException,因为您的 == 运算符将导致无限递归。
编辑:
如果 Complex 是一个结构体,那么 NullReferenceExceptions 就不会有任何问题。如果 Complex 是一个类,您可以更改 == 和 != 运算符重载的实现以避免异常(Laurent Etiemble 已经在他的回答中指出了这一点):
You can use the following at the top of your Equals override:
The exception you are getting is probably a StackOverflowException because your == operator will cause infinite recursion.
EDIT:
If Complex is a struct you should not have any problems with NullReferenceExceptions. If Complex is a class you can change your implementation of the == and != operator overloads to avoid the exception (Laurent Etiemble already pointed this out in his answer):
您应该考虑在运算符重载中使用静态 Equals 方法(这将调用实例 Equals 方法):
注意:您还可以在 Equals 方法中检查
null
。您还可以阅读 MSDN 上的 Object.Equals 主题< /a>,这是一个很好的样本来源。
You should consider using the static Equals method in the operator overloads (which will call the instance Equals method):
Note: You may also check for
null
in the Equals method.You can also read the Object.Equals Topic on MSDN, which is a great source of samples.
穷人的单元测试
Poor man's unit test
我认为你应该在 == 运算符实现中测试 null 。否则,当 lhs 为 null 时,您将调用 Complex(null).Equals (我不知道 C# 中的情况,但在 Java 中这将是一个 Nullpointer 异常)
要测试 null,我建议类似:
So Object.Equals上面所有的 == 比较都会被调用。
I think you shoud test for null in the == operator implementation. Otherwise, when lhs is null, you'd call Complex(null).Equals (I don't know for C#, but in Java this would be a Nullpointer Exception)
To test for null, I suggest something like:
So Object.Equals will be called for all == comparisons above.
有一个更好的方法,然后使用运算符
is
和cast
:以下是关于
Equals
运算符覆盖并与null< 进行比较的快速测试/code>:
调试不会进入操作员的主体:
There is a better approach then using operators
is
andcast
:Here is a quick test concerning
Equals
operator override and comparing withnull
:Debugging doesn't step into operator's body: