简体   繁体   中英

Question on C# Variable Scope vs. Other Languages

First of all, let me say that I've never used C# before, and I don't know about it much.

I was studying for my "Programming Languages" exam with Sebesta's "Concepts of Programming Languages 9th ed" book. After I read the following excerpt from "Scope declaration order (on 246th page)", I got a little bit puzzled:

"...For example, in C99, C++, Java the scope of all local variables is from their declarations to the ends of the blocks in which those declarations appear. However, in C# the scope of any variable declared in a block is the whole block, regardless of the position of the declaration in the block, as long as it is not in a nested block. The same is true for methods. Note that C# still requires that all variables be declared before they are used. Therefore, although the scope of a variable extends from the declaration to the top of the block or subprograms in which that declaration appears, the variable still cannot be used above its declaration "

Why did designers of C# make such decision? Is there any specific reason/advantage for such an unusual decision?

Have a look at Eric Lippert's posts about declaration spaces , which give some more background to these rules.

I'd normally quote the most relevant bit when linking to blog posts, but I think these really give a better answer when all read together.

Hopefully he'll pop by and give a good summary.

This prevents you from doing something such as

void Blah()
{
   for (int i = 0; i < 10; i++)
   {
      // do something
   }

   int i = 42;
}

The reason is that it introduces the possibility for subtle bugs if you have to move code around, for instance. If you need i before your loop, now your loop is broken.

减少混淆的好处之一是,如果变量声明上方有一个嵌套块,则变量声明将生效并防止嵌套块声明同名变量。

From the C# Spec

class A
{
    int i = 0;
    void F() {
        i = 1;                  // Error, use precedes declaration
        int i;
        i = 2;
    }
    void G() {
        int j = (j = 1);        // Valid
    }
    void H() {
        int a = 1, b = ++a; // Valid
    }
}

The scoping rules for local variables are designed to guarantee that the meaning of a name used in an expression context is always the same within a block. If the scope of a local variable were to extend only from its declaration to the end of the block, then in the example above, the first assignment would assign to the instance variable and the second assignment would assign to the local variable, possibly leading to compile-time errors if the statements of the block were later to be rearranged.

It's not that strange. As far as variables go, it enforces unique naming better than Java/C++.

Eric Lippert's answer on this related question might be of help.

As Anthony Pegram said earlier, C# enforces this rule because there are cases where rearranging the code can cause subtle bugs, leading to confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM