I have class B that has class A, this is simplified version of class A and B.
class A
{
int x;
int y;
public A(int x, int y)
{
this.x = x;
this.y = y;
}
public A()
{
x = 0;
y = 0;
}
...
}
class B
{
A a;
public B(A a)
{
this.a = a;
}
public B()
{
this.a = null;
}
public A getA()
{
return a;
}
...
}
And I need to compare the object a as follows.
public class MyClass
{
public static void RunSnippet()
{
var a = new A(10, 20);
var b = new B(a);
Console.WriteLine(a == b.getA());
}
}
The a == b.getA() was always true, but after syncing to the new A and B, now a != b.getA(). I compared the element by element of a, and b.getA() using debugger, but they seem to be the same.
Is there any way to compare the reference (the address) of a and b.getA()?
With C/C++, I could easily get the pointer value, but I don’t know how can I do that with C#.
You can use Object.ReferenceEquals for this. If you don’t override
Equalsor==it will produce the same result.ex
ReferenceEquals(a, b.getA()