简体   繁体   English

属性或Equals和GetHashCode中的字段?

[英]Properties or fields in Equals and GetHashCode?

I'm using a mix of Jon Skeet's GetHashCode and the MSDN Guidelines for Overloading Equals to implement my Equals and GetHashCode methods. 我混合使用了Jon Skeet的GetHashCodeMSDN过载等于准则,以实现我的Equals和GetHashCode方法。 However, one thing has been bothering me. 但是,有一件事一直困扰着我。

Should I implement them checking against the actual fields in the class, or their properties? 我是否应该根据类中的实际字段或它们的属性来实现它们?

Furthermore, what about a parent class's properties with fields that are private (thus I can't access them) - should I be checking those as well, and then should I check them via their properties? 此外,具有私有字段的父类的属性又如何呢(因此我无法访问它们)-我也应该检查它们,然后再通过其属性检查它们吗?

I guess I'll make my comment an answer. 我想我的评论将成为答案。

It really depends on your definition of equals on how detailed you should implement it. 这实际上取决于您对等值的定义,以及对它的详细程度。 Properties are nothing more than over-glorified fields. 属性不过是过度夸张的字段。 I probably wouldn't hash a read-only, calculated property. 我可能不会散列只读的计算属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM