Skip to content

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,
        };
    }
}

使用重载后的 + 操作符

cs
Complex c1 = new Complex()
{
    Value = 1,
};

Complex c2 = new Complex()
{
    Value = 2,
};

Complex c3 = c1 + c2; // 3

C#的一元操作符及其相容于CLS的方法名

C# 操作符特殊方法名推荐的相容于 CLS 的方法名
+op_UnaryPlusPlus
-op_UnaryNegationNegate
!op_LogicalNotNot
~op_OnesComplementOnesComplement
++op_IncrementIncrement
--op_DecrementDecrement
(无)op_TrueIsTrue { get; }
(无)op_FalseIsFalse { get; }

C#的二元操作符及其相容于CLS的方法名

C# 操作符特殊方法名推荐的相容于 CLS 的方法名
+op_AdditionAdd
-op_SubtractionSubtract
*op_MultiplyMultiply
/op_DivisonDivide
%op_ModulusMod
&op_BitwiseAndBitwiseAnd
op_BitwiseOrBitwiseOr
^op_ExclusiveOrXor
<<op_LeftShiftLeftShift
>>op_RightShiftRightShift
==op_EqualityEquals
!=op_InequalityEquals
<op_LessThenCompare
>op_GreaterThenCompare
<=op_LessThenOrEqualCompare
>=op_GreaterThenOrEqualCompare

附: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