[英]Overriding Equality Operators
I've implemented a class that overloads the ==
and !=
operators. 我已经实现了一个重载
==
和!=
运算符的类。
This seems to work fine; 这似乎工作正常; however, I get the warning
'type' defines operator == or operator != but does not override Object.Equals(object o)
. 但是,我得到警告
'type' defines operator == or operator != but does not override Object.Equals(object o)
。
Okay, so I implemented Equals. 好的,所以我实施了Equals。 But now I get the warning
'type' defines operator == or operator != but does not override Object.GetHashCode()
. 但现在我得到警告
'type' defines operator == or operator != but does not override Object.GetHashCode()
。
Does this end at some point? 这会在某个时候结束吗? Or have I wandered into a never-ending trail of requirements just because I want to overload
==
and !=
? 或者我是否因为我想要超载
==
和!=
而徘徊在一个永无止境的需求线索中?
Does this end at some point?
这会在某个时候结束吗?
Yes, once you implement GetHashCode
it will end. 是的,一旦你实现了
GetHashCode
它就会结束。 Eric Lippert has blogged about its importance . Eric Lippert在博客中发表了关于其重要性的博文 。 All I can do is suggest you to read and trust him :-)
我能做的就是建议你阅读并信任他:-)
Yes, it will end when you override GetHashCode
. 是的,它将在您重写
GetHashCode
时结束。
When implementing equality operators, and furthermore Equals
, it is the responsibility of the programmer to provide an implementation to deliver a custom hash code for that type. 在实现相等运算符以及
Equals
,程序员有责任提供实现来为该类型提供自定义哈希代码。
See this MSDN reference for details. 有关详细信息,请参阅此MSDN参考 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.