Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Overloading the comparison operator, how to compare if the two variables points to the same object(i.e. not 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);
}
like image 890
Hao Avatar asked Mar 06 '09 05:03

Hao


1 Answers

Use the Object.ReferenceEquals static method.

Of course, in order for the == and != method to retain their full functionality, you should also be overriding Equals and GetHashCode so that they return a consistent set of responses to callers.

like image 51
Steve Mitcham Avatar answered Oct 03 '22 06:10

Steve Mitcham