关于 Equal Override Overload 和 IEquatable

namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
Point2D a = new Point2D
{
X = ,
Y =
}; 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) 可以避免一次装箱
 
上一篇:Android Volley完全解析(二),使用Volley加载网络图片


下一篇:jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片)