views:

223

answers:

6

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?

+3  A: 

One example of a benefit of reduced confusion is that if you have a nested block above the variable declaration, the variable declaration will be in effect and prevent the nested block from declaring a variable with the same name.

David Gladfelter
+7  A: 

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.

Greg Beech
I think the given answers already give a good summary, but in short: these rules are designed to make code easier to read, reason about, and refactor safely, and thereby prevent bugs.
Eric Lippert
+4  A: 

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.

Anthony Pegram
+1  A: 

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

Andy_Vulhop
+3  A: 

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.

Conrad Frix
A: 

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.

Zach Johnson