Difference between pre-increment and post-increment in a loop?

后端 未结 22 1772
暗喜
暗喜 2020-11-21 23:41

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?

22条回答
  •  盖世英雄少女心
    2020-11-22 00:34

    As this code shows (see the dissambled MSIL in the comments), the C# 3 compiler makes no distinction between i++ and ++i in a for loop. If the value of i++ or ++i were being taken, there would definitely be a difference (this was compiled in Visutal Studio 2008 / Release Build):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace PreOrPostIncrement
    {
        class Program
        {
            static int SomethingToIncrement;
    
            static void Main(string[] args)
            {
                PreIncrement(1000);
                PostIncrement(1000);
                Console.WriteLine("SomethingToIncrement={0}", SomethingToIncrement);
            }
    
            static void PreIncrement(int count)
            {
                /*
                .method private hidebysig static void  PreIncrement(int32 count) cil managed
                {
                  // Code size       25 (0x19)
                  .maxstack  2
                  .locals init ([0] int32 i)
                  IL_0000:  ldc.i4.0
                  IL_0001:  stloc.0
                  IL_0002:  br.s       IL_0014
                  IL_0004:  ldsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                  IL_0009:  ldc.i4.1
                  IL_000a:  add
                  IL_000b:  stsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                  IL_0010:  ldloc.0
                  IL_0011:  ldc.i4.1
                  IL_0012:  add
                  IL_0013:  stloc.0
                  IL_0014:  ldloc.0
                  IL_0015:  ldarg.0
                  IL_0016:  blt.s      IL_0004
                  IL_0018:  ret
                } // end of method Program::PreIncrement             
                 */
                for (int i = 0; i < count; ++i)
                {
                    ++SomethingToIncrement;
                }
            }
    
            static void PostIncrement(int count)
            {
                /*
                    .method private hidebysig static void  PostIncrement(int32 count) cil managed
                    {
                      // Code size       25 (0x19)
                      .maxstack  2
                      .locals init ([0] int32 i)
                      IL_0000:  ldc.i4.0
                      IL_0001:  stloc.0
                      IL_0002:  br.s       IL_0014
                      IL_0004:  ldsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                      IL_0009:  ldc.i4.1
                      IL_000a:  add
                      IL_000b:  stsfld     int32 PreOrPostIncrement.Program::SomethingToIncrement
                      IL_0010:  ldloc.0
                      IL_0011:  ldc.i4.1
                      IL_0012:  add
                      IL_0013:  stloc.0
                      IL_0014:  ldloc.0
                      IL_0015:  ldarg.0
                      IL_0016:  blt.s      IL_0004
                      IL_0018:  ret
                    } // end of method Program::PostIncrement
                 */
                for (int i = 0; i < count; i++)
                {
                    SomethingToIncrement++;
                }
            }
        }
    }
    

提交回复
热议问题