将变量声明为更接近定义的位置? C#IL代码不一样 [英] Declare the variable closer to where it is defined? C# IL code is not the same

查看:115
本文介绍了将变量声明为更接近定义的位置? C#IL代码不一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我想知道是否应该将varbile clsoer声明为使用它的地方,是的,有多个问题问这个问题.但是我仍然有疑问!

In C#, I was wondering if we should declare the varbile clsoer to where it is used or not and yes there are multiple questions asked this same question. But still I have doubts!

使用我尝试过的示例代码:

with a sample code like this I tried:

private void button1_Click(object sender, EventArgs e)
{
    for(int i= 0; i<=1000; i++)
    {
        int myVariable = 12;
        myVariable = i;
    }
}

另一次我将代码行移动为int myVariable = 12;在for循环之前.这些就是我在ILDASM中得到的,前提是问题和答案都说C#创建了相同的代码,但是在这里我们看到的并非如此!那么发生了什么?最后,这是一个好习惯吗? -使用C#编译器-

the other time I moved the line of code for int myVariable = 12; to right before the for loop. And these are what I get in ILDASM, previsouds questions and answers say that C# created the same code, but here we see that not really! So what is going on? Finally is it a good practice or not? - with C# compiler -

内部变量:

.method private hidebysig instance void  button1_Click(object sender,
                                                       class [mscorlib]System.EventArgs e) cil managed
{
  // Code size       32 (0x20)
  .maxstack  2
  .locals init ([0] int32 i,
           [1] int32 myVariable,
           [2] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  ldc.i4.0
  IL_0002:  stloc.0
  IL_0003:  br.s       IL_0010
  IL_0005:  nop
  IL_0006:  ldc.i4.s   12
  IL_0008:  stloc.1
  IL_0009:  ldloc.0
  IL_000a:  stloc.1
  IL_000b:  nop
  IL_000c:  ldloc.0
  IL_000d:  ldc.i4.1
  IL_000e:  add
  IL_000f:  stloc.0
  IL_0010:  ldloc.0
  IL_0011:  ldc.i4     0x3e8
  IL_0016:  cgt
  IL_0018:  ldc.i4.0
  IL_0019:  ceq
  IL_001b:  stloc.2
  IL_001c:  ldloc.2
  IL_001d:  brtrue.s   IL_0005
  IL_001f:  ret
} /

/方法Form1 :: button1_Click的结尾

/ end of method Form1::button1_Click

在for循环之外的变量:

.method private hidebysig instance void  button1_Click(object sender,
                                                       class [mscorlib]System.EventArgs e) cil managed
{
  // Code size       32 (0x20)
  .maxstack  2
  .locals init ([0] int32 myVariable,
           [1] int32 i,
           [2] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   12
  IL_0003:  stloc.0
  IL_0004:  ldc.i4.0
  IL_0005:  stloc.1
  IL_0006:  br.s       IL_0010
  IL_0008:  nop
  IL_0009:  ldloc.1
  IL_000a:  stloc.0
  IL_000b:  nop
  IL_000c:  ldloc.1
  IL_000d:  ldc.i4.1
  IL_000e:  add
  IL_000f:  stloc.1
  IL_0010:  ldloc.1
  IL_0011:  ldc.i4     0x3e8
  IL_0016:  cgt
  IL_0018:  ldc.i4.0
  IL_0019:  ceq
  IL_001b:  stloc.2
  IL_001c:  ldloc.2
  IL_001d:  brtrue.s   IL_0008
  IL_001f:  ret
} // end of method Form1::button1_Click

推荐答案

良好实践完全取决于您的需求 !如果需要不同的变量(循环的每次迭代),请在循环内部创建它,否则应将其放在外部.

Good practice depends entirely on what your needs are! If you need a different variable, each iteration of the loop, you create it inside the loop, else you should put it outside.

这篇关于将变量声明为更接近定义的位置? C#IL代码不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆