Why use GetHashCode() over Equals()?

前端 未结 5 519
既然无缘
既然无缘 2020-12-11 16:09

HashSet.Add first compares the results of GetHashCode. If those are equal, it calls Equals.

Now, my understanding is

5条回答
  •  [愿得一人]
    2020-12-11 16:54

    GetHashCode() gets you an integral value that you can use for hashtables. That hash code is one reason why hashtables are so performant. However there can be more than one objects with the same hashcode. That's why Equals() is called. If the objects are not equal, they can go into the same bucket, if they are equal, then it is already in the hashtable and does not need to be added.

提交回复
热议问题