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

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

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

9条回答
  •  囚心锁ツ
    2021-01-11 19:13

    Overriding == to make it call Equals strikes me as a generally bad idea for reference types. If you override == to make it call Equals, then I don't think there's a way for a user of your code to test whether two object references refer to the exact same object (versus an object with equal properties).

    If people want to test instances of your classes for value equality then surely they should just call Equals, saving == for testing reference equality specifically.

提交回复
热议问题