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

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
Click For Summary
The discussion centers on the implementation of the Object.Equals method in C#. The inquiry highlights the confusion regarding how equality is generalized at a low level and seeks the source code for this method. It is noted that Object.Equals primarily checks if two references point to the same memory location, effectively acting as ReferenceEquals for reference types. For structures, however, Object.Equals performs a member-wise value equality test, which is more complex. The conversation emphasizes the importance of overriding the Equals method in custom classes to define specific equality semantics. It also cautions against making assumptions about the implementation details, as subclasses may override the method, and documentation should be the primary source of information regarding its behavior.
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?
 
Technology 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 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.
 

Similar threads

Replies
81
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K