Solved

What is the difference between == and ReferenceEquals()

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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