namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
Point2D a = new Point2D
{
X = 1,
Y = 2
};
Point2D b = a;
CallEquals(a, b);
a.Equals(b);
}
public static void CallEquals<T>(T instance, T other)
{
instance.Equals(other);
}
}
public struct Point2D
{
public int X;
public int Y;
public override bool Equals(object obj)
{
if (!(obj is Point2D)) return false;
Point2D other = (Point2D) obj;
return X == other.X && Y == other.Y;
}
public bool Equals(Point2D other)
{
return X == other.X && Y == other.Y;
}
}
}
CallEquals(a, b); 走 public override bool Equals(object obj)
a.Equals(b); 走 public bool Equals(Point2D other)
如果 struct Point2D 继承 IEquatable 则都会走 public bool Equals(Point2D other) 可以避免一次装箱