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

后端 未结 7 1762
离开以前
离开以前 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:04

    In Java a ==b check if the references of the two objects are equals (rougly, if the two objects are the same object "aliased")

    a.equals(b) compare the values represented by the two objects.

    0 讨论(0)
  • 2021-02-01 05:14

    I believe that a == b will check if the referenced object is the same.

    Usually to see if the value is the same a.Equals(b) is used (this often needs to be overridden in order to work).

    0 讨论(0)
  • 2021-02-01 05:17

    You pretty much answered your question yourself:

    I have also over-ridden Equals() in the subclass implementation, which does a very basic check to see whether or not the passed-in object is null and is of the subclass's type. If both those conditions are met, the objects are deemed to be equal.

    The == operator hasn't been overloaded - so it's returning false since a and b are different objects. But a.Equals is calling your override, which is presumably returning true because neither a nor b are null, and they're both of the subclass' type.

    So your question was "When can a == b be false and a.Equals(b) true?" Your answer in this case is: when you explicitly code it to be so!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 05:19

    The "==" operate tests absolute equality (unless overloaded); that is, it tests whether two objects are the same object. That's only true if you assigned one to the other, ie.

    MyObject a = MyObject.Create();
    MyObject b = a;
    

    Just setting all the properties of two objects equal doesn't mean the objects themselves are. Under the hood, what the "==" operator is comparing is the addresses of the objects in memory. A practical effect of this is that if two objects are truly equal, changing a property on one of them will also change it on the other, whereas if they're only similar ("Equals" equal), it won't. This is perfectly consistent once you understand the principle.

    0 讨论(0)
  • 2021-02-01 05:24

    a == b checks if they reference the same object.

    a.Equals(b) compares the contents.

    This is a link to a Jon Skeet article from 2004 that explains it better.

    0 讨论(0)
提交回复
热议问题