C# 相等性检查
您为您创建的结构
和类
编写相等性检查的方法是什么?
1)“完整”是否相等性检查需要大量样板代码(例如覆盖Equals
、覆盖GetHashCode
、通用Equals
、operator==
,运算符!=
)?
2) 您是否明确指定您的类对 IEquatable
接口进行建模?
3) 我是否理解正确,当我调用诸如 a == b
之类的内容时,没有实际的方法可以自动应用 Equals
覆盖我总是必须同时实现 Equals
和 operator==
成员?
What's your approach on writing equality checks for the structs
and classes
you create?
1) Does the "full" equality checking require that much of boilerplate code (like override Equals
, override GetHashCode
, generic Equals
, operator==
, operator!=
)?
2) Do you specify explicitly that your classes model the IEquatable<T>
interface?
3) Do I understand correctly, that there is no actual way to automatically apply Equals
overrides, when I invoke something like a == b
and I always have to implement both the Equals
and operator==
members?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是对的,这是很多样板代码,你需要单独实现所有内容。
我建议:
GetHashCode
和Equals(object)
- 创建 == 的重载并实现IEquatable
T>
如果不这样做可能会导致非常意外的行为,Equals(object)
和,我总是会实现
IEquatable
GetHashCodeIEqualityComparer
来表达您感兴趣的比较。这是一个示例实现:
是的,这是大量的样板文件,实现之间的变化很少:(
==
的实现稍微比可能是这样,因为它会调用Equals(object)
,它需要进行动态类型检查......但替代方案甚至更加样板,如下所示:You're right, this is a lot of boiler-plate code, and you need to implement everything separately.
I would recommend:
GetHashCode
andEquals(object)
- creating overloads for == and implementingIEquatable<T>
without doing that could result in very unexpected behaviourIEquatable<T>
if you're overridingEquals(object)
andGetHashCode
IEqualityComparer<T>
expressing the comparison you're interested in.Here's a sample implementation:
And yes, that's a heck of a lot of boilerplate, very little of which changes between implementations :(
The implementation of
==
is slightly less efficient than it might be, as it will call through toEquals(object)
which needs to do the dynamic type check... but the alternative is even more boiler-plate, like this:我很少在课堂上做任何特别的事情;对于大多数常规对象,引用相等效果很好。
我什至很少写
struct
;但由于结构代表值,因此通常适合提供相等性等。这通常会涉及所有内容; Equals、==、!= 和IEquatable
(因为这可以避免在使用EqualityComparer.Default
的场景中进行装箱。样板文件通常不会有太大问题,但是像 resharper 这样的 IIRC 工具可以在这里提供帮助。
是的,建议保持 Equals 和 == 同步,并且需要明确地完成此操作。
I rarely do anything special for classes; for most regular objects referential equality works fine.
I even more rarely write a
struct
; but since structs represent values it is usually appropriate to provide equality etc. This would usually involve everything; Equals, ==, != andIEquatable<T>
(since this avoids boxing in scenarios usingEqualityComparer<T>.Default
.The boilerplate isn't usually too problematic, but IIRC tools like resharper can help here.
Yes, it is advisable to keep Equals and == in sync, and this needs to be done explicitely.
您只需要为 a==b 实现operator==。
由于我喜欢字典中的数据,有时我会重写 GetHashCode。
接下来,我实现 Equals(作为未提及的标准...这是因为使用泛型时没有相等性约束)并指定实现 IEquatable。既然我要这样做,我不妨将我的 == 和 != 实现指向 Equals。 :)
You just need to implement operator== for a==b .
As I like my data in dictionaries sometimes I override GetHashCode.
Next I implement Equals (as an unmentioned standard ... this is because there is no constraint for equality when using generics) and specify implementing IEquatable. Since I am going to do this I might as well point my == and != implementations to Equals. :)
请参阅什么是“最佳实践”比较引用类型的两个实例?
借助代码片段 这是一个这样的..
See What is "Best Practice" For Comparing Two Instances of a Reference Type?
You can avoid boiler plate code (hope C#/VS team brings something easy for developers in their next iteration) with the help of a snippet, here is one such..