Is it possible to combine hash codes for private members to generate a new hash code?

后端 未结 4 1036
野趣味
野趣味 2020-12-05 08:21

I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable.

The code should be the

4条回答
  •  不知归路
    2020-12-05 08:48

    There is nothing wrong with this approach as long as the members whose hashcodes you are combining follow the rules of hash codes. In short ...

    1. The hash code of the private members should not change for the lifetime of the object
    2. The container must not change the object the private members point to lest it in turn change the hash code of the container

提交回复
热议问题