Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

What is the difference between == and ReferenceEquals()

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
mowerm
Asked:
mowerm
2 Solutions
 
God_AresCommented:
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
 
gregoryyoungCommented:
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
 
a_goatCommented:
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
 
God_AresCommented:
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
 
mowermAuthor Commented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now