GetHashCode() problem using xor

前端 未结 2 1124
萌比男神i
萌比男神i 2021-02-14 07:08

My understanding is that you\'re typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here\'s

2条回答
  •  醉梦人生
    2021-02-14 08:02

    Andrew has posted a good example for generating a better hash code, but also bear in mind that you shouldn't use hash codes as an equality check, since they are not guaranteed to be unique.

    For a trivial example of why this is consider a double object. It has more possible values than an int so it is impossible to have a unique int for each double. Hashes are really just a first pass, used in situations like a dictionary when you need to find the key quickly, by first comparing hashes a large percentage of the possible keys can be ruled out and only the keys with matching hashes need to have the expense of a full equality check (or other collision resolution methods).

提交回复
热议问题