IComparable and Equals()

后端 未结 3 1817
攒了一身酷
攒了一身酷 2020-12-10 15:57

From MSDN

Types that implement IComparable must override Equals.Types that override Equals must also override GetHashCode; otherwise, Hashtable migh

3条回答
  •  自闭症患者
    2020-12-10 16:37

    There are two ways that objects in your code could be compared: Equals and GetHashCode

    For your object to be compared properly in ALL situations, when you override the Equals method (used for some comparisons), you must also override GetHashCode (used in the rest).

    If you override one but not the other, depending on your code, you could get unexpected results.

提交回复
热议问题