Solved

What is the difference between == and ReferenceEquals()

Posted on 2004-09-07
5
400 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now