Solved

What is the difference between == and ReferenceEquals()

Posted on 2004-09-07
5
402 Views
Last Modified: 2012-05-05
I'm a Java programmer coming to C# (although I did C++ in the dim dark past) and I'm having a problem with ReferenceEquals().

My reading so far doesn't seem to indicate any difference in functionality between the use of ReferenceEquals( a, b ) and a == b.

The only thing I can think of is that operator overloading allows you to change the semantics of == and hence ReferenceEquals() is the only way to guarantee that you are comparing references to instances.

I'd really like the definitive answer on this one as I am uncertain about a lot of things in C# and I don't want references and equality to be amongst them.

Many thanks,

Matt

Without knowing if there are subtle aspects to the answer i've no idea how difficult or easy this one is.
0
Comment
Question by:mowerm
5 Comments
 
LVL 7

Assisted Solution

by:God_Ares
God_Ares earned 50 total points
ID: 11995228
Simple == is an operator and just like c++ you can implement one for your class. For example the == for a tring doesn't check for referece (pointer into memory) but the actual content.

here is a bit of code that should explain a lot to you

object we = new object();
                  object wee = new object();
                  if (we==wee)
                  {
                        MessageBox.Show("yes");
                  }
                  else
                  {
                        MessageBox.Show("no");
                  }

                  string a = "he dude";
                  string b = "he dudee";
                  b=b.Remove(7,1);

                  if (ReferenceEquals(a,b))
                  {
                        MessageBox.Show("yes");
                  }
                  else
                  {
                        MessageBox.Show("no");
                  }

                  string aa = "he dude";
                  string ba = "he dude"; //yes both  aa and ba are going to point to the same string!! this is an optimalisation
                  

                  if (ReferenceEquals(aa,ba))
                  {
                        MessageBox.Show("yes");
                  }
                  else
                  {
                        MessageBox.Show("no");
                  }

                  if (a==b)
                  {
                        MessageBox.Show("yes");
                  }
                  else
                  {
                        MessageBox.Show("no");
                  }

this should explain a lot.

regards

FLoris
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 50 total points
ID: 11997820
much more simply ...

referenceequals() compares the pointer (reference) where the physical object is stored

equals() or == if not overriden from object base will compare the pointer (reference) to where to object is stored but CAN be overriden to do value based comparisons.
0
 
LVL 3

Expert Comment

by:a_goat
ID: 12044946
Ummmm God Ares.  You need to read up a bit more.  The string == operator does check pointers.  The reason you can use it to compare strings is that strings are interned.  That means that you can have the following:

string a = "foo";           // Memory: 0000 -> "foo"
string b = "bar";           // Memory: 0004 -> "bar"
string c = "foobar";      // Memory: 0008 -> "foobar"
string d = a + b;          // Memory: 0008 -> "foobar"

That's why it's a really bad idea to append strings often.  If you create a string of a-z by adding each letter in turn, you will actually end up with 26 strings of 26 different lengths stuck in memory.  That's why it's always a good idea to use the StringBuilder when building long complicated strings.
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 12052103
are you saying that c==b? you are right,.. but it's Not checking pointers. If you disagree  please enlighten us why ReferenceEquals(d,c) returns false.

well there could be an optimalisation (i would do it) where the first overriden check of == would be a ReferenceEquals, so the compare does not have to run throug the complete string.. k
but if it returns false it still runs trough the string.
0
 

Author Comment

by:mowerm
ID: 12070100
Thanks Gregory & Floris.  I think the answer I was looking for was the confirmation that ReferenceEquals() is required since you cannot depend upon the behaviour of operator ==.  I've split the points, hope that's okay.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to iterate over object and get property name and value 4 26
c# LinkButton OnClientClick 2 34
c# string handling 1 27
Device Location 4 19
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now