简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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