Why use GetHashCode() over Equals()?

前端 未结 5 527
既然无缘
既然无缘 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:52

    Because if an algorithm wants to test if 1 object is already in a set of 1.000.000 objects, it has to call Equals 1.000.000 times, but GetHashCode() just once (and a few calls to Equals to eliminate objects which are different though having the same hash code).

提交回复
热议问题