Is it necessary to override == and != operators when overriding the Equals method? (.NET)

后端 未结 9 1184
一向
一向 2021-01-11 19:06

Or it\'s advisable to do that? Why?

9条回答
  •  北海茫月
    2021-01-11 19:34

    See Guidelines for Implementing Equals and the Equality Operator (==)

    For Value Types (structs) "Implement == any time you override the Equals method"

    For Reference Types (classes), "Most reference types, even those that implement the Equals method, should not override ==." The exception is for immutable classes and those with value-like semantics.

提交回复
热议问题