Class Methods - "Equals" and "ReferenceEquals"

Mr_Fulano
Mr_Fulano used Ask the Experts™
on
Hi, I'm using MS VS2010, C#.NET. I have what may be somewhat of an academic question.

I created a custom Class in one of my applications. As a default, the Class has two methods in it. Those being the "Equals" and "ReferenceEquals" methods.

Why is this a default? I would assume that my custom Class is getting the methods through inheritance from perhaps the Object Class, but why would any Class want to have such methods as defaults?

Thanks for your help,
Fulano
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
I would assume that my custom Class is getting the methods through inheritance from perhaps the Object Class...
Exactly.

Determining equality between two objects is just one of those things that happens so often, it sort of makes sense that every "thing" in .NET implement a method to determine such. Since every "thing" in .NET is descended from the object class, and every "thing" in .NET inherits what the object class defines, every "thing" in .NET has an Equals method.

ReferenceEquals is actually a new one for me (not to imply that it is by any means "new" to the Framework). The difference between the two is that Equals is an instance method which can be overridden, and ReferenceEquals is a static method (which cannot be overridden). Whereas you can change the behavior of Equals to say that two objects are equal by something other than their respective references (e.g. some property, like "Name", determines equality), with ReferenceEquals you are dealing strictly with the references. In other words, ReferenceEquals says, "are these two variables pointing to the same object in memory?" Overriding the Equals method can give you a shortcut to checking whether or not an entry exists within a collection; using ReferenceEquals can tell you whether or not the two variables actually point to the same exact object (in terms of location in memory).
anarki_jimbelSenior Developer

Commented:
I just give you an example about strings.

String is a class, you know.

We declare

string s1 = "hello";
string s2 = "hello";

If you call Equals method - they are equal! (Just because that the Equals is overridden for strings).
And, obviously, references are not same - these are two different objects.

In most cases Equals and ReferenceEquals return the same result. But not for strings and some other objects.

And, of course, you may override the behaviour for your custom class.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
@anarki_jimbel

Your example is flawed because string literals are interned. In your example, those two string actually are the same references!

e.g.

Console.WriteLine(object.ReferenceEquals(s1, s2));

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
An example of where the two strings would not be the same reference:

string s1 = typeof(string).Name;
string s2 = "String";

Console.WriteLine(string.Equals(s1, s2));
Console.WriteLine(string.ReferenceEquals(s1, s2));

Open in new window

anarki_jimbelSenior Developer

Commented:
Agreed, you are right.

Author

Commented:
Gentlemen, thank you both....and very nicely stated! Very good analysis Kaufmed. Your explanation is very well stated and well supported with your examples. Thank you for sharing and helping me understand that...it was puzzling me as to why I would want those methods, but you actually helped me see deeper into the theory of the code.

Good job!

Fulano

Author

Commented:
Excellent explanation!
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
NP. Glad to help  = )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial