When can a == b be false and a.Equals(b) true?

后端 未结 7 1771
离开以前
离开以前 2021-02-01 04:59

I ran into this situation today. I have an object which I\'m testing for equality; the Create() method returns a subclass implementation of MyObject.

MyObject         


        
7条回答
  •  既然无缘
    2021-02-01 05:18

    They both do the same unless they are specifically overloaded within the object to do something else.

    A quote from the Jon Skeet Article mentioned elsewhere.

    The Equals method is just a virtual one defined in System.Object, and overridden by whichever classes choose to do so. The == operator is an operator which can be overloaded by classes, but which usually has identity behaviour.

    The keyword here is USUALLY. They can be written to do whatever the underlying class wishes and in no way do they have to do the same.

提交回复
热议问题