相关疑难解决方法(0)

运算符重载和不同类型

我有一个课程分数,它将大量用于比较整数.我打算重载==运算符以根据下面的代码启用这些比较?

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是运算符重载的明智用法吗?

我是否应该为操作员的LH和RH侧提供过载以允许使用对称?

c#

9
推荐指数
2
解决办法
5337
查看次数

标签 统计

c# ×1