如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现;
示例:
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 = 10 }; Salary roseIncome = new Salary { RMB = 11 }; Salary familyIncome = mikeIncome + roseIncome;
通过使用operator关键字定义静态成员函数来重载运算符。