【发布时间】:2009-11-01 20:48:37
【问题描述】:
我只是在看Guidelines for Overloading Equals() on msdn(见下面的代码);大部分我都清楚,但有一条线我不明白。
if ((System.Object)p == null)
或者,在第二个覆盖中
if ((object)p == null)
为什么不简单
if (p == null)
反对购买我们的因素是什么?
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);
}
public bool Equals(TwoDPoint p)
{
// If parameter is null return false:
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return (x == p.x) && (y == p.y);
}
【问题讨论】: