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

  • Context: C# 
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
8 replies · 2K views
SlurrerOfSpeech
Messages
141
Reaction score
11
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?
 
Physics news on Phys.org
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
 
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.
 
jedishrfu said:
If you read the remarks section of Object.Equals it says that Object.Equals is the same as ReferenceEquals ...

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.
 
Filip Larsen said:
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.

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?
 
jedishrfu said:
The OP asked for the implementation of Object.Equals and that would only compare the locations in memory of the two objects.

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.
 
Filip Larsen said:
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.

Yes, that's what I was asking.
 
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.
 
  • Like
Likes   Reactions: Silicon Waffle
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.