在C#中的变量范围 [英] Scope of variables in C#

查看:153
本文介绍了在C#中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了你们对C#code写了一个问题:
我有我的collegue长时间的辩论:
他说,通过声明和initilizing所有变量中的功能使code之初的更可读快速

I got a question for you guys on C# code writing: I had a long debate with collegue of mine: He says that by declaring and initilizing all variables in the beginning of the function make code more readable and fast.

请给我至少有一个健全的原因,也可能是对的。

Please give me at least one sane reason that it could be right.

1 这不是可读的,如果你有很长的函数,你必须有后intialization范围巨大申报范围,所以这是很容易声明你需要在你需要它的变量,所以你会看到它在你的弗朗。

1. It's not readable as if you have a long function, you have to have huge declaration scope after intialization scope, so this is much easier to declare the variable you need where you need it, so you will see it in fron of you.

2 这不是绝对的速度更快,因为你分配不必要的内存和泵功能堆栈。

2. It's not absolutely faster, as you allocate unnecessary memory and pump the function stack.

这个伪code例如我的:

This pseudo code example mine:

public void A(double dParam) 
{         
    if(... condition ... ) {
        double dAnotherParam;
        string sParam; 
        ...
        // use local scope vars here
     }   
}

伪code例如他:

Pseudo code example him:

public void A(double dParam) 
{
    double dAnotherParam;
    string sParam; 

    dAnotherPatam = 0; 
    sParam = null;     

    if(... condition ... ) {
        ...
    }                   
 }

在题目任何想法?

Any ideas on topic?

感谢您提前。

推荐答案

这是一个风格问题。我的考虑:

This is a matter of style. My considerations:


  1. 如果在code座小,到最后都不会有问题,你放置的声明。然而,这是preferable有报关单靠近的使用,主要是在大code块。首先,这种方式可以确保该变量没有被修改,直到块点,二是因为你可以检查变量快的类型和初始化。

  1. If the code block is small, in the end it won't matter where you place the declaration. However, it's preferable to have declarations nearer use, mainly in large code blocks. First, this way you can make sure that the variable wasn't modified until that point in the block, and second, because you can check the type and initialization of the variable faster.

此外,它通过声明不远的地方使用它们变数更快的 code 的 - 你没有向后滚动的范围年初刚刚宣布被遗忘的变量,然后向后移动。

Besides, its faster to code by declaring variables near where you use them -- you don't have to scroll back to the beginning of the scope just to declared a forgotten variable and then move back.

这是不是更快或更慢。变量,无论在范围内声明,都会有自己的分配和初始化的范围开始集中。这可以通过产生的IL的语言进行检查。

This isn't faster or slower. Variables, no matter where declared in the scope, will have their allocation and initialization centered at the beginning of the scope. This can be checked by generating IL for the language.

例如,code:

static void Main(string[] args)
{
    var a = 3;

    if (a > 1)
    {
        int b = 2;
        a += b;
    }

    var c = 10;
    Console.WriteLine(a + c);   
 }

生成以下IL:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       24 (0x18)
  .maxstack  2
  .locals init ([0] int32 a,     // Declare a
           [1] int32 b,          // Declare b
           [2] int32 c)          // Declare c
  IL_0000:  ldc.i4.3             
  IL_0001:  stloc.0              
  IL_0002:  ldloc.0              
  IL_0003:  ldc.i4.1             
  IL_0004:  ble.s      IL_000c
  IL_0006:  ldc.i4.2
  IL_0007:  stloc.1
  IL_0008:  ldloc.0
  IL_0009:  ldloc.1
  IL_000a:  add
  IL_000b:  stloc.0
  IL_000c:  ldc.i4.s   10
  IL_000e:  stloc.2
  IL_000f:  ldloc.0
  IL_0010:  ldloc.2
  IL_0011:  add
  IL_0012:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0017:  ret
} // end of method Program::Main

要注意一个重要的事情是,在内部范围,如变量b,它是在如果范围变量是在由编译器法范围内声明。

One important thing to notice is that variables in internal scopes, such as the variable b, which was at the if scope, is declared at method scope by the compiler.

希望它帮助。

这篇关于在C#中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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