C# 操作符重载
🏷️ 《CLR via C#》
重载 +
操作符
cs
sealed class Complex
{
public int Value { get; set; }
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex()
{
Value = c1.Value + c2.Value,
};
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
使用重载后的 +
操作符
cs
Complex c1 = new Complex()
{
Value = 1,
};
Complex c2 = new Complex()
{
Value = 2,
};
Complex c3 = c1 + c2; // 3
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
C#的一元操作符及其相容于CLS的方法名
C# 操作符 | 特殊方法名 | 推荐的相容于 CLS 的方法名 |
---|---|---|
+ | op_UnaryPlus | Plus |
- | op_UnaryNegation | Negate |
! | op_LogicalNot | Not |
~ | op_OnesComplement | OnesComplement |
++ | op_Increment | Increment |
-- | op_Decrement | Decrement |
(无) | op_True | IsTrue { get; } |
(无) | op_False | IsFalse { get; } |
C#的二元操作符及其相容于CLS的方法名
C# 操作符 | 特殊方法名 | 推荐的相容于 CLS 的方法名 |
---|---|---|
+ | op_Addition | Add |
- | op_Subtraction | Subtract |
* | op_Multiply | Multiply |
/ | op_Divison | Divide |
% | op_Modulus | Mod |
& | op_BitwiseAnd | BitwiseAnd |
丨 | op_BitwiseOr | BitwiseOr |
^ | op_ExclusiveOr | Xor |
<< | op_LeftShift | LeftShift |
>> | op_RightShift | RightShift |
== | op_Equality | Equals |
!= | op_Inequality | Equals |
< | op_LessThen | Compare |
> | op_GreaterThen | Compare |
<= | op_LessThenOrEqual | Compare |
>= | op_GreaterThenOrEqual | Compare |
附:Complex 类的 IL 代码
cs
.class /*02000002*/ private auto ansi sealed beforefieldinit ConsoleApp1.Complex
extends [mscorlib/*23000001*/]System.Object/*01000010*/
{
.field /*04000001*/ private int32 '<Value>k__BackingField'
.custom /*0C000002:0A00000F*/ instance void [mscorlib/*23000001*/]System.Runtime.CompilerServices.CompilerGeneratedAttribute/*01000011*/::.ctor() /* 0A00000F */ = ( 01 00 00 00 )
.custom /*0C000003:0A000010*/ instance void [mscorlib/*23000001*/]System.Diagnostics.DebuggerBrowsableAttribute/*01000013*/::.ctor(valuetype [mscorlib/*23000001*/]System.Diagnostics.DebuggerBrowsableState/*01000012*/) /* 0A000010 */ = ( 01 00 00 00 00 00 00 00 )
.method /*06000001*/ public hidebysig specialname
instance int32 get_Value() cil managed
// SIG: 20 00 08
{
.custom /*0C000001:0A00000F*/ instance void [mscorlib/*23000001*/]System.Runtime.CompilerServices.CompilerGeneratedAttribute/*01000011*/::.ctor() /* 0A00000F */ = ( 01 00 00 00 )
// 方法在 RVA 0x2048 处开始
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: /* 02 | */ ldarg.0
IL_0001: /* 7B | (04)000001 */ ldfld int32 ConsoleApp1.Complex/*02000002*/::'<Value>k__BackingField' /* 04000001 */
IL_0006: /* 2A | */ ret
} // end of method Complex::get_Value
.method /*06000002*/ public hidebysig specialname
instance void set_Value(int32 'value') cil managed
// SIG: 20 01 01 08
{
.custom /*0C000012:0A00000F*/ instance void [mscorlib/*23000001*/]System.Runtime.CompilerServices.CompilerGeneratedAttribute/*01000011*/::.ctor() /* 0A00000F */ = ( 01 00 00 00 )
// 方法在 RVA 0x2050 处开始
// 代码大小 8 (0x8)
.maxstack 8
IL_0000: /* 02 | */ ldarg.0
IL_0001: /* 03 | */ ldarg.1
IL_0002: /* 7D | (04)000001 */ stfld int32 ConsoleApp1.Complex/*02000002*/::'<Value>k__BackingField' /* 04000001 */
IL_0007: /* 2A | */ ret
} // end of method Complex::set_Value
.method /*06000003*/ public hidebysig specialname static
class ConsoleApp1.Complex/*02000002*/
op_Addition(class ConsoleApp1.Complex/*02000002*/ c1,
class ConsoleApp1.Complex/*02000002*/ c2) cil managed
// SIG: 00 02 12 08 12 08 12 08
{
// 方法在 RVA 0x205c 处开始
// 代码大小 31 (0x1f)
.maxstack 4
.locals /*11000001*/ init ([0] class ConsoleApp1.Complex/*02000002*/ V_0)
IL_0000: /* 00 | */ nop
IL_0001: /* 73 | (06)000004 */ newobj instance void ConsoleApp1.Complex/*02000002*/::.ctor() /* 06000004 */
IL_0006: /* 25 | */ dup
IL_0007: /* 02 | */ ldarg.0
IL_0008: /* 6F | (06)000001 */ callvirt instance int32 ConsoleApp1.Complex/*02000002*/::get_Value() /* 06000001 */
IL_000d: /* 03 | */ ldarg.1
IL_000e: /* 6F | (06)000001 */ callvirt instance int32 ConsoleApp1.Complex/*02000002*/::get_Value() /* 06000001 */
IL_0013: /* 58 | */ add
IL_0014: /* 6F | (06)000002 */ callvirt instance void ConsoleApp1.Complex/*02000002*/::set_Value(int32) /* 06000002 */
IL_0019: /* 00 | */ nop
IL_001a: /* 0A | */ stloc.0
IL_001b: /* 2B | 00 */ br.s IL_001d
IL_001d: /* 06 | */ ldloc.0
IL_001e: /* 2A | */ ret
} // end of method Complex::op_Addition
.method /*06000004*/ public hidebysig specialname rtspecialname
instance void .ctor() cil managed
// SIG: 20 00 01
{
// 方法在 RVA 0x2087 处开始
// 代码大小 8 (0x8)
.maxstack 8
IL_0000: /* 02 | */ ldarg.0
IL_0001: /* 28 | (0A)000011 */ call instance void [mscorlib/*23000001*/]System.Object/*01000010*/::.ctor() /* 0A000011 */
IL_0006: /* 00 | */ nop
IL_0007: /* 2A | */ ret
} // end of method Complex::.ctor
.property /*17000001*/ instance int32 Value()
{
.get instance int32 ConsoleApp1.Complex/*02000002*/::get_Value() /* 06000001 */
.set instance void ConsoleApp1.Complex/*02000002*/::set_Value(int32) /* 06000002 */
} // end of property Complex::Value
} // end of class ConsoleApp1.Complex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79