Interesting behaviour of type “decimal” in C#

前端 未结 5 1111
遇见更好的自我
遇见更好的自我 2021-02-12 13:34

If we declare padding as const decimal, the padding is not working.

mymoney = 1.2 and your money = 1.20, how can this behavior be explained?

class Progra         


        
5条回答
  •  悲&欢浪女
    2021-02-12 13:56

    As an accompaniment to Jon's answer, below is the IL produced from your code. As he mentioned, mymoney was never added.

    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       61 (0x3d)
      .maxstack  6
      .locals init ([0] valuetype [mscorlib]System.Decimal balance,
               [1] valuetype [mscorlib]System.Decimal padding,
               [2] valuetype [mscorlib]System.Decimal mymoney,
               [3] valuetype [mscorlib]System.Decimal yourmoney)
      IL_0000:  nop
      IL_0001:  ldc.i4.s   12
      IL_0003:  ldc.i4.0
      IL_0004:  ldc.i4.0
      IL_0005:  ldc.i4.0
      IL_0006:  ldc.i4.1
      IL_0007:  newobj     instance void [mscorlib]System.Decimal::.ctor(int32,
                                                                         int32,
                                                                         int32,
                                                                         bool,
                                                                         uint8)
      IL_000c:  stloc.0
      IL_000d:  ldc.i4.0
      IL_000e:  ldc.i4.0
      IL_000f:  ldc.i4.0
      IL_0010:  ldc.i4.0
      IL_0011:  ldc.i4.2
      IL_0012:  newobj     instance void [mscorlib]System.Decimal::.ctor(int32,
                                                                         int32,
                                                                         int32,
                                                                         bool,
                                                                         uint8)
      IL_0017:  stloc.1
      IL_0018:  ldloc.0
      IL_0019:  ldc.i4.2
      IL_001a:  call       valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::Round(valuetype [mscorlib]System.Decimal,
                                                                                              int32)
      IL_001f:  stloc.2
      IL_0020:  ldloc.0
      IL_0021:  ldloc.1
      IL_0022:  call       valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Addition(valuetype [mscorlib]System.Decimal,
                                                                                                    valuetype [mscorlib]System.Decimal)
      IL_0027:  ldc.i4.2
      IL_0028:  call       valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::Round(valuetype [mscorlib]System.Decimal,
                                                                                              int32)
      IL_002d:  stloc.3
      IL_002e:  ldloc.2
      IL_002f:  call       void [mscorlib]System.Console::WriteLine(valuetype [mscorlib]System.Decimal)
      IL_0034:  nop
      IL_0035:  ldloc.3
      IL_0036:  call       void [mscorlib]System.Console::WriteLine(valuetype [mscorlib]System.Decimal)
      IL_003b:  nop
      IL_003c:  ret
    } // end of method Program::Main
    

    To produce the IL (i.e. if you want to look under the hood in the future), just run ILDASM from a VS command prompt, then load your executable and double-click on the method that you would like to look at.

提交回复
热议问题