473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Difference between Convert.ReferenceEquals() and Object.Equals()

Today, I ran into something I didn't quite expect. I'm sure there is
a logical explanation for it so I want to post it and see if anyone
can explain the difference to me.

I came across a situation when I wanted to see if a public variable
inside a struct was null or not. Sample code below:

namespace SampleConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Init
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyStruct theStruct = new MyStruct();
theStruct.Variable1 = null;

try
{
if (theStruct.Variable1.Equals(null))
Console.WriteLine(".Equals evaluated");
}
catch
{
}

try
{
if (Convert.ReferenceEquals(theStruct.Variable1, null))
Console.WriteLine("ReferenceEquals evaluated");
}
catch
{
}
}
}
public struct MyStruct
{
public string Variable1;
public string Variable2;
}
}

What I found out was that theStruct.Variable1.Equals(null) throws an
System.NullReferenceException while
Convert.ReferenceEquals(theStruct.Variable1, null) returns true.
Well, there is clearly a difference between the two methods.

Looking in the MSDN library, I found the following definitions for the
two methods:

Object.Equals method: Determines whether two Object instances are
equal.
Convert.ReferenceEquals method: Determines whether the specified
Object instances are the same instance.

Well, I fail to see the difference in the two definitions. Being the
engineer that I am, I thought well let me look up the definition of a
System.NullReferenceException and the MSDN library offers:

The exception that is thrown when there is an attempt to dereference a
null object reference.

This doesn't help any either. Any thoughts?
Nov 15 '05 #1
2 4057
Kevin... First. From a design point of view
someReference.Equals(someReference) is ugly since calling a method on
a null reference variable will throw a nullreferenceexception forcing
you
to either use an "assert" or check for null. A better design would be
static Equals(referenceOne, referenceTwo) which works even if one or
both references are null.

ReferenceEquals(o1,o2) uses this superior design and determines if two
references refer to the same object (referential equality). Therefore,
it
does not throw a nullreferenceexception even if one or both of the
operands are null.

Your class is supposed to override Equals to compare two objects by
value for _content_ equivalence. The base class Object has no way to
determine how to do this so the default implementation compares
objects by reference! Ugggg.

So. There is no guarantee what Equals does in a class. It should return
content equivalence, but it might return referential equality. It is
poorly
designed as it can throw a nullreferenceexception.

So. How about virtual Equals(o1,o2) in the next iteration of C#?

Regards,
Jeff
Object.Equals method: Determines whether two Object instances are

equal. Convert.ReferenceEquals method: Determines whether the
specified Object instances are the same instance. Well, I fail to see
the
difference in the two definitions.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #2
Woops. There already is an Equals(o1,o2). My bad.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Holbox | last post by:
Hi people, look at this (console project) sub main dim a as string = "Hola" dim b as string = "Hola" dim c as string = new string("Hola") dim d as string = new string("Hola")
6
by: Daniel Billingsley | last post by:
In C# these are equivalent, right? if (ReferenceEquals(objectA, objectB)) { } and if (objectA == objectB) {
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
20
by: Marcel Hug | last post by:
Hi NG! In my book I have the following code simple and I tested it: public class Base : ICloneable { public int Age; public string Name; public Base(string myname)
10
by: Bruce | last post by:
I posted this on the dotnet.languages.vc group but did not get a response. I figure that I could apply what I learn from CSharp to VC, so here goes. I have a class in a class assembly, that...
17
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
What is the difference of == and Object.Equals() If I want to query werther to pointers are pointing to same instance, do I use == or Objects.Equals? foo a,b; if ( a == b ) ... or if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.