After overloading the == operator, how to compare if two variables point to the same object?

Overloading a comparison operator, how to compare if two variables point to the same object (i.e. not a value)

public static bool operator ==(Landscape a, Landscape b)
{
    return a.Width == b.Width && a.Height == b.Height;
}

public static bool operator !=(Landscape a, Landscape b)
{
    return !(a.Width == b.Width && a.Height == b.Height);
}
+5
source share
4 answers

Use the static method Object.ReferenceEquals.

Of course, in order for the == and! = Method to retain their full functionality, you must also override Equals and GetHashCode so that they return a consistent set of answers to callers.

+8
source

, . Object.ReferenceEquals. true, . false

+4

, == Object.Equals, ! =.

, , <, > , < =, > =.

, , , , - CodeRush Refactor, "" ( ), , 25 , . , ==, ! =, .

0

All Articles