What's the best strategy for Equals and GetHashCode?

后端 未结 7 1967
你的背包
你的背包 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:20

    Assuming that the instances are equal because the hash codes are equal is wrong.

    I guess your implementation of GetHashCode is OK, but I usually use things similar to this:

    public override int GetHashCode() {
        return object1.GetHashCode ^ intValue1 ^ (intValue2 << 16);
    }
    

提交回复
热议问题