重载的运算符参数始终为 null
所以我有一个类,它重写 Equals(object obj) 和 GetHashCode() 以及实现 IEquatable。为了在检查相等性时使使用这种类型更加自然,我想,哎呀,我会重载相等运算符和不等运算符,不用担心......
呃哦,担心......考虑以下内容 - 其中两个 myType 实例不为空:
if (myType != container.myType) //NullReferenceException
{
//never get here
}
//never get here either
现在,容器只是另一个类,用于保存 myType 的实例以及用于缓存项目的其他内容。
这是来自 myType 的实际(相关)代码:
public class MyType : IEquatable<MyType>
{
public static bool operator ==(MyType myTypeA, MyType myTypeB)
{
return myTypeA.Equals(myTypeB);
}
public static bool operator !=(MyType myTypeA, MyType myTypeB)
{
return !(myTypeA == myTypeB);
}
public override bool Equals(object obj)
{
if (obj != null && obj is MyType)
{
return Equals((MyType)obj);
}
return false;
}
public bool Equals(MyType other)
{
if (other != null)
{
return other.ToString() == ToString();
}
return false;
}
}
在这方面有任何经验吗?
谢谢。
So I have a class which overrides Equals(object obj) and GetHashCode() along with implementing IEquatable. To make working with this type a little more natural when checking for equality I thought, heck, I'd overload the equality operator and inequality operator, no worries...
Uh oh, worries... consider the following - where both myType instances are NOT null:
if (myType != container.myType) //NullReferenceException
{
//never get here
}
//never get here either
Now, container is just another class to hold an instance of myType among other things which is used for caching items.
Here's the actual (relevant) code from myType:
public class MyType : IEquatable<MyType>
{
public static bool operator ==(MyType myTypeA, MyType myTypeB)
{
return myTypeA.Equals(myTypeB);
}
public static bool operator !=(MyType myTypeA, MyType myTypeB)
{
return !(myTypeA == myTypeB);
}
public override bool Equals(object obj)
{
if (obj != null && obj is MyType)
{
return Equals((MyType)obj);
}
return false;
}
public bool Equals(MyType other)
{
if (other != null)
{
return other.ToString() == ToString();
}
return false;
}
}
Any experience on this front?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,您需要仔细检查空值,因为它会再次调用您的相等函数,通常会导致 StackOverflowException。
当我在类上使用 IEquatable 接口时,我通常使用以下代码:
As the others have stated you need to be carefull checking for nulls as it will call your equality function again, normally resulting in a StackOverflowException.
When I use the
IEquatable
interface on classes I normally use the following code:几个指针 -
==
和!=
,请确保使用ReferenceEquals
检查重载内是否为 null实现而不是==
,因为这会调用重载的运算符,然后进入循环或尝试在 nullthis
引用上调用Equals
,这可能就是这里发生的情况。==
和!=
。这些运算符旨在实现值相等,而类并不是真正设计为具有值相等。删除运算符重载,或将MyType
设为结构体。Couple of pointers -
==
and!=
on classes, make sure to useReferenceEquals
to check for null inside the overload implementations rather than==
, as that will call your overloaded operator and either go into a loop or try to callEquals
on a nullthis
reference, which is probably what is happening here.==
and!=
on classes. Those operators are meant for value equality, and classes aren't really designed to have value equality. Either remove the operator overloads, or makeMyType
a struct.棘手的一个...问题是您在 Equal 覆盖中使用相等运算符,如下所示:
它转到您重载的 != 运算符,该运算符又转到您的 == 运算符,该运算符试图执行 null.Equals...
Tricky one... the problem is that you use the equality operator inside the Equal override as follows:
It goes to your overloaded != operator, which in turn goes to your == operator, which trying to do null.Equals...