What's the best strategy for Equals and GetHashCode?

后端 未结 7 1942
你的背包
你的背包 2020-11-28 11:25

I\'m working with a domain model and was thinking about the various ways that we have to implement these two methods in .NET. What is your preferred strategy?

This i

相关标签:
7条回答
  • 2020-11-28 12:27

    Hashcodes can collide so I don't think they are a good way to compare equality. You should compare the underlying values that make the objects "equal" instead. See @Jon Skeet's answer to this question: What is the best algorithm for an overridden System.Object.GetHashCode? for a better GetHashCode implementation if your equality encompasses several properties. If it's just a single property, you can just reuse it's hashcode.

    0 讨论(0)
提交回复
热议问题