Consider the following code
public class Rectangle : IEquatable<Rectangle>
{
public int Width { get; set; }
public int Height { get; set; }
public bool Equals(Rectangle other)
{
return Width == other.Width
&& Height == other.Height;
}
}
IEquatable<Rectangle> a = new Rectangle() { Width = 10, Height = 20 };
IEquatable<Rectangle> b = new Rectangle() { Width = 10, Height = 20 };
a.Equals(b); // returns false;
I know the value semantics of this class are awful (sorry). I normally follow the C# guidelines for IEquatable<T>
implementation, which state that object.Equals(object obj)
also needs to be overridden. - This is just speculation.
Since a and b are declared as IEquatable<Rectangle>
, why when I call Equals
, is it calling object.Equals(object obj)
, rather than IEquatable<Rectangle>.Equals(Rectangle other)
?
It seems a little odd to use the object
implementation over the IEquatable<T>
implementation (again, I know I SHOULD override the classes default Equals(object obj)
implementation...this is just speculation as to why this happens)