Equals, GetHashCode, EqualityComparers and fuzzy equality

前端 未结 3 1667
离开以前
离开以前 2021-01-20 07:12

For an object with properties A, B, C, D, StartDate and EndDate if I wanted to implement something where any two objects are equal if they have identical A, B and C and over

3条回答
  •  一生所求
    2021-01-20 07:31

    If you are using the items in a hashtable then their GetHashCode method will be used initially to test for equality and if their hashes are found to be equal a call to their Equals method will be made to establish whether they are equal.

    From the documentation:

    "But is that enough for the Hashtable to determine they are identical objects? Unfortunately, no. If the Hashtable finds two objects with the same hash, it calls their Equals method to see whether the two objects are in fact equal. Again, the default implementation of Object.Equals will return false if the two objects are two different instances of the same class. So we need to also add an override of the Equals method to our class.

提交回复
热议问题