Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What is the implementation of Object.Equals in C#?

  1. Feb 16, 2016 #1
    Excuse me if this is a dumb question, but where can I find the implementation of Object.Equals, https://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx. I'm curious how one can write a procedure that takes an Object (or another class that is a Object) and compares whether it's "equal" to another, how this concept of equality can be so generalized at a low level. Where can I find the source code for that function?
     
  2. jcsd
  3. Feb 16, 2016 #2

    Filip Larsen

    User Avatar
    Gold Member

    Even though you can find a fair amount of the source code on [1] the Object.Equals method delegates to some helper code that (as far as I know) is not included on that site. The functionality, as described on the page you linked to, is a core part of the platform and I assume its implemented in unmanaged code, and as such that code will not necessarily be possible in C# directly.

    If you where to make functionality in C# similar to what is described on the Object.Equals page you linked to, then one candidate would be to use reflection [2].

    [1] http://referencesource.microsoft.com/
    [2] https://msdn.microsoft.com/en-us/library/f7ykdhsy(v=vs.110).aspx
     
  4. Feb 16, 2016 #3

    jedishrfu

    Staff: Mentor

    If you read the remarks section of Object.Equals it says that Object.Equals is the same as ReferenceEquals so basically it tests if two references to objects are actually the same object ie reside at the same location in memory.

    Other notions of equals where the contents of one object are equal to the contents of another objects aren't handled by this Object.Equals method.
     
  5. Feb 16, 2016 #4

    Filip Larsen

    User Avatar
    Gold Member

    Note, that this is only true for reference types (classes). For structures, which also inherit Object.Equals, object equality is a member-wise value equality test which I assume is what the OP is asking how is implemented.
     
  6. Feb 16, 2016 #5

    jedishrfu

    Staff: Mentor

    The OP asked for the implementation of Object.Equals and that would only compare the locations in memory of the two objects. Subclasses of Object may choose to implement something more. Does that sound right?
     
  7. Feb 16, 2016 #6

    Filip Larsen

    User Avatar
    Gold Member

    It sounds to me like you are thinking of objects as only class instances. My point is, that the Object.Equals implementation do something "trivial" for class instances and something "non trivial" for struct instances. I assume the OP asks about how to do the later.
     
  8. Feb 16, 2016 #7
    Yes, that's what I was asking.
     
  9. Feb 23, 2016 #8

    harborsparrow

    User Avatar
    Gold Member

    When you define your own classes, you generally ought to override .Equals and make it mean whatever you want, so that people can compare objects safely for equality.

    Maybe that's obvious; I was a little confused about what was really being asked.

    For library classes, the semantics of .Equals should appear in the documentation.
     
  10. Feb 24, 2016 #9
    You should only know what's provided for you in the documentation. Making assumptions about how things get done is not a good idea because you have no guarantee that it will stay that way. You also have to assume that some subclasses of Object have overridden that method.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: What is the implementation of Object.Equals in C#?
  1. What to do with c++? (Replies: 6)

Loading...