G
Guest
Hi !
I come up from a raw C/C++ background and since the pointers in C# are not
so powerfull, i have some diffuculties to understand whats going on behind on
..NET platform. So my question is:
"How can I understand if two references are pointing to the same
adress/object ?"
I found a method "ReferenceEquals", but did not understand , how it works,
cause it behaves a bit strangly and the example of using it on msdn is not
satisfing.
So my example :
object foo = 5;
object cfoo = foo;
bool ok = Object.ReferenceEquals(foo, cfoo);
////////////////////////////////// i get OK
/////////////////////////////////////
if (ok)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
////////////////////////////////// i get OK
/////////////////////////////////////
if (foo == cfoo)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
////////why is the value of cfoo 5 if foo and cfoo are pointing to the same
object///
foo = 6;
Console.WriteLine(foo); // 6
Console.WriteLine(cfoo); // 5
////////////////////////////////// now i get not OK
/////////////////////////////////////
if (ok)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
////////////////////////////////// now i get not OK
/////////////////////////////////////
if (foo == cfoo)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
so, it seems so both the ==operator and the ReferenceEquals method are just
comparing the values and not if the instances are same.
Am I wrong ? I am highly confused about that and need little help
I come up from a raw C/C++ background and since the pointers in C# are not
so powerfull, i have some diffuculties to understand whats going on behind on
..NET platform. So my question is:
"How can I understand if two references are pointing to the same
adress/object ?"
I found a method "ReferenceEquals", but did not understand , how it works,
cause it behaves a bit strangly and the example of using it on msdn is not
satisfing.
So my example :
object foo = 5;
object cfoo = foo;
bool ok = Object.ReferenceEquals(foo, cfoo);
////////////////////////////////// i get OK
/////////////////////////////////////
if (ok)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
////////////////////////////////// i get OK
/////////////////////////////////////
if (foo == cfoo)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
////////why is the value of cfoo 5 if foo and cfoo are pointing to the same
object///
foo = 6;
Console.WriteLine(foo); // 6
Console.WriteLine(cfoo); // 5
////////////////////////////////// now i get not OK
/////////////////////////////////////
if (ok)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
////////////////////////////////// now i get not OK
/////////////////////////////////////
if (foo == cfoo)
Console.WriteLine("OK");
else
Console.WriteLine("not OK");
so, it seems so both the ==operator and the ReferenceEquals method are just
comparing the values and not if the instances are same.
Am I wrong ? I am highly confused about that and need little help