我们可以使用他来实现这样的需求:
当一个订单与另外一个订单相加形成一个新的订单。新订单的货物为两个订单的货物的并集,总价为两个订单的总价和。
下面实现看看
先做个辅助的货物类






















下面是客户端调用代码



















贴上MSDN的说明
operator 关键字用于在类或结构声明中声明运算符。运算符声明可以采用下列四种形式之一:
public static result-type operator unary-operator ( op-type operand ) public static result-type operator binary-operator ( op-type operand, op-type2 operand2 ) public static implicit operator conv-type-out ( conv-type-in operand ) public static explicit operator conv-type-out ( conv-type-in operand ) |
参数
- result-type
-
运算符的结果类型。
- op-type
-
第一个(或唯一一个)参数的类型。
- operand
-
第一个(或唯一一个)参数的名称。
- op-type2
-
第二个参数的类型。
- operand2
-
第二个参数的名称。
- conv-type-out
-
类型转换运算符的目标类型。
- conv-type-in
-
类型转换运算符的输入类型。
备注
前两种形式声明了用户定义的重载内置运算符的运算符。注意,并非所有内置运算符都可以被重载(请参见可重载的运算符)。op-type 和 op-type2 中至少有一个必须是封闭类型(即运算符所属的类型)。例如,这将防止重定义整数加法运算符。
后两种形式声明了转换运算符。conv-type-in 和 conv-type-out 中正好有一个必须是封闭类型(即,转换运算符只能从它的封闭类型转换为其他某个类型,或从其他某个类型转换为它的封闭类型)。
任何运算符声明的前面都可以有一个可选的属性(C# 编程指南)列表。