When Should a .NET Class Override Equals()? When Should it Not?

后端 未结 4 1103
小鲜肉
小鲜肉 2020-11-30 10:20

The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part

Overriding operator == in non-immut

4条回答
  •  野性不改
    2020-11-30 11:07

    Check out the Guidelines and rules for GetHashCode by Eric Lippert.

    Rule: the integer returned by GetHashCode must never change while the object is contained in a data structure that depends on the hash code remaining stable

    It is permissible, though dangerous, to make an object whose hash code value can mutate as the fields of the object mutate.

提交回复
热议问题