Should variable declarations always be placed outside of a loop?

前端 未结 6 1405
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 15:39

Is it better to declare a variable used in a loop outside of the loop rather then inside? Sometimes I see examples where a variable is declared inside the loop. Does this ef

6条回答
  •  春和景丽
    2020-12-20 15:53

    As is the case with lots of simple optimizations like this, the compiler takes care of it for you. If you try both of these and look at the assemblies' IL in ildasm you can see that they both declare a single int32 read variable, although it does reorder the declarations:

      .locals init ([0] int32 read,
               [1] uint8[] buffer,
               [2] bool CS$4$0000)
    
      .locals init ([0] uint8[] buffer,
               [1] int32 read,
               [2] bool CS$4$0000)
    

提交回复
热议问题