【发布时间】:2010-10-11 16:36:09
【问题描述】:
重载比较运算符,如果两个变量指向同一个对象(即不是值),如何比较
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);
}
【问题讨论】:
-
我尽量避免为引用类型重载这些运算符,因为大多数人都希望 == 来测试对象相等性。
标签: c# operators operator-overloading equals-operator