tags:

views:

68

answers:

5

Hello everybody

I am just curious background of some classes and methods in .NET so i just wonder how ReferenceEquals method can understand 2 references belongs to same object or not?

+4  A: 

Because 'the same object' means 'same place in memory' and the operation can simply check the 'address' value of a reference.

Not that you can easily get at that value, but then you never need to.

Henk Holterman
How can i get 'address' value of object ?
Freshblood
You can't. It happens under the hood, but that doesn't mean it's not there. A reference is in fact a kind of safeguarded garbage collected pointer, and the difference betweed ReferenceEquals and regular Equals is that ReferenceEquals checks the pointer itself, while Equals checks what it points to.
tdammers
@Fresh, I added a Link
Henk Holterman
@Henk Holterman - I coudln't understand from given links. I tried ways in answers but still couldn't get. Can u spesificy which class and method helps to get it ?
Freshblood
FreshBlood, basically you _cannot_ get it. More or less because you never need to (it's totally meaningless in a normal program) nobody bothered to provide a way. Use the debugger to look at it.
Henk Holterman
@Henk Holterman - Purpose of question was understand it not doing anything with it. I think it is easy get that value in unmanaged environment but it is hided in managed environment. At least i understand that it is integer value which is adress of memory.
Freshblood
I understand, but platform design is focused on usage, not study.
Henk Holterman
@Henk Holterman - You wrote that "Not that u can easily.." but i understand i can get that value easily. I missunderstood it. My poor english can caused problem.
Freshblood
I could have written that better. English is not my 1st language either.
Henk Holterman
A: 

I'd suspect it's checking to see whether the references point to the same object in memory, i.e. is the same address being referenced for both objects.

Lazarus
+1  A: 

Because they have the same value. Just like two pointers in an unmanaged app point to the same object if they have the same value.

Marcelo Cantos
+2  A: 

The reference is basically a 32-bit integer which points to a location in the heap where the object is stored, so ReferenceEquals can simply compare the two integers values and check if they're the same.

Mind you for value types, ReferenceEqual will ALWAYS fail! ValueType overrides the virtual Object.Equals method to compare all member variables in a derived type rather than the reference.

With out knowing the runtime names and types of the member variables, the default implementation of ValueType.Equals relies on the use of reflection and reflection as we all know, is slow. As a general rule of thumb, it’s recommended that you ALWAYS override the ValueType.Equals method when creating your custom value type and you should strongly consider overloading the == and != operators too whilst you’re at it!

theburningmonk
Can i get that integer value ?
Freshblood
@theburningmonk - Considerable knowledge about value type equality.
Freshblood
Looks like Henk's already answer that question
theburningmonk
@Fresh - read it up on the Effective C# book Bill Wagner wrote, highly recommended book!
theburningmonk
+1  A: 

ReferenceEqulas checks the memory location is same or not

 class MyClass {

       static void Main() {
          object o = null;
          object p = null;
          object q = new Object();

          //Here o and p not referring to any location.so result true
          Console.WriteLine(Object.ReferenceEquals(o, p));
          p = q; // here p's memory location is mapping to q.So both referring to same memory    location. so result is true.
          Console.WriteLine(Object.ReferenceEquals(p, q));
            //Here the p and o are in different memory location so the result is false
          Console.WriteLine(Object.ReferenceEquals(o, p));
       }
    }
anishmarokey