如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现;

  示例:

class Salary
{
public int RMB { get; set; } public static Salary operator +(Salary s1, Salary s2)
{
return new Salary { RMB = s1.RMB + s2.RMB };
}
} //调用
Salary mikeIncome = new Salary { RMB = };
Salary roseIncome = new Salary { RMB = };
Salary familyIncome = mikeIncome + roseIncome;

  通过使用operator关键字定义静态成员函数来重载运算符。

04-26 08:22