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

  • Context: C# 
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the implementation of the Object.Equals method in C#, exploring how equality is determined for objects and the underlying mechanics of this method. Participants express curiosity about the source code and the differences in equality handling between reference types and value types.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires about the source code for Object.Equals and how it generalizes the concept of equality for objects.
  • Another participant notes that Object.Equals delegates to helper code likely implemented in unmanaged code, which may not be accessible in C#.
  • It is mentioned that Object.Equals is equivalent to ReferenceEquals, which checks if two references point to the same memory location, applicable primarily to reference types.
  • Some participants clarify that for structures, Object.Equals performs a member-wise value equality test, which may differ from the behavior for reference types.
  • There is a suggestion that subclasses of Object may implement their own equality logic, which could differ from the default behavior.
  • One participant emphasizes the importance of overriding .Equals in user-defined classes to ensure meaningful equality comparisons.
  • Concerns are raised about making assumptions regarding the implementation details of Object.Equals, highlighting the potential for changes in subclasses.

Areas of Agreement / Disagreement

Participants generally agree on the distinction between how Object.Equals operates for reference types versus value types, but there is no consensus on the specifics of its implementation or the implications of overriding this method in subclasses.

Contextual Notes

Some participants express uncertainty about the implementation details, particularly regarding the handling of equality for different types and the potential for subclasses to override the default behavior.

Who May Find This Useful

This discussion may be useful for C# developers interested in understanding object equality, the implementation of Object.Equals, and best practices for overriding equality in custom classes.

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   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.
 

Similar threads

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