Solved

What is the difference between == and ReferenceEquals()

Posted on 2004-09-07
5
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
"Emulate" TAB key when press Enter Key 3 70
Where does legacy ASP.NET initialize language? 2 59
Need to sort columns in DataGridView 4 36
Selenium and Xpath 4 30
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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