在浏览MSDN关于Equals重写的文档时,有一点引起了我的注意。
在这个页面的示例中,进行比较时进行了一些空值检查,并将对象转换为System.Object类型:
public override bool Equals(System.Object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to Point return false.
TwoDPoint p = obj as TwoDPoint;
if ((System.Object)p == null)
{
return false;
}
// Return true if the fields match:
return (x == p.x) && (y == p.y);
}
有没有使用这种转换方式的特定原因,还是只是在这个例子中遗忘了一些“无用”的代码?