【发布时间】:2011-08-14 03:51:56
【问题描述】:
我有一个班级分数,它将在与整数的比较中大量使用。我打算按照下面的代码重载 == 运算符以启用这些比较?
public class Score
{
public Score(int score) {
Value = score;
}
public static bool operator ==(Score x, int y) {
return x != null && x.Value == y;
}
public static bool operator ==(int y, Score x)
{
return x != null && x.Value == y;
}
}
这是对运算符重载的合理使用吗?
我是否应该为运算符的 LH 和 RH 侧提供重载以允许使用对称?
【问题讨论】:
标签: c#