重載比較運算,如何比較,如果兩個變量指向同一個對象(即沒有值)重載operator ==後,如果兩個變量指向同一個對象,如何比較?
public static bool operator ==(Landscape a, Landscape b)
{
return a.Width == b.Width && a.Height == b.Height;
}
public static bool operator !=(Landscape a, Landscape b)
{
return !(a.Width == b.Width && a.Height == b.Height);
}
我儘量避免重載這些運算符的引用類型,因爲大多數人會期望==來測試對象的相等性。 – 2009-03-06 07:05:08