I need to check if two objects of the same type are the same instances and point to the same allocation of memory. The problem is that the type has overloaded equality operator and thus it will use it as comparing the both for equality, but I need to check them for reference. I looked through object.ReferenceEquals()
method, but it internally applies equality operator
Operators can't be overridden - they can only be overloaded.
So the ==
operator in object.ReferenceEquals
is still comparing references, or you could do the same thing yourself by casting one or both operands:
string x = "some value";
string y = new string(x.ToCharArray());
Console.WriteLine(x == y); // True
Console.WriteLine((object) x == (object) y); // False
Console.WriteLine(ReferenceEquals(x, y)); // False
See more on this question at Stackoverflow