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

  • C#
  • Thread starter SlurrerOfSpeech
  • Start date
In summary, the Object.Equals method delegates to some helper code that 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.
  • #1
SlurrerOfSpeech
141
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
  • #2
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
 
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
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.
 
  • #7
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.
 
  • #8
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
  • #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.
 

What is Object.Equals in C#?

Object.Equals is a method in the C# programming language that allows for the comparison of two objects to determine if they are equal or not.

How does Object.Equals work?

Object.Equals compares two objects by checking if they refer to the same instance or if they have the same values for their properties or fields. It uses reflection to compare values, making it a more comprehensive comparison than the "==" operator.

What is the syntax for using Object.Equals in C#?

The syntax for using Object.Equals is "object1.Equals(object2)". This will return a boolean value of true if the objects are equal and false if they are not.

Can Object.Equals be overridden?

Yes, Object.Equals can be overridden in a class to provide a custom implementation for comparing objects of that class. This can be useful when comparing complex objects with different properties and fields.

Are there any limitations to using Object.Equals?

One limitation of using Object.Equals is that it cannot compare objects of different types. It also does not take into account the state of the object, meaning two objects with the same values but in different states may not be considered equal.

Similar threads

  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
924
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
1
Views
949
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
4
Views
4K
Back
Top