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

后端 未结 4 1104
小鲜肉
小鲜肉 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:01

    The MSDN documentation about not overloading == for mutable types is wrong. There is absolutely nothing wrong for mutable types to implement equality semantics. Two items can be equal now even if they will change in the future.

    The dangers around mutable types and equality generally show up when they are used as a key in a hash table or allow mutable members to participate in the GetHashCode function.

提交回复
热议问题