Team LiB
Previous Section Next Section

Code Blocks and Variable Scope

C# (like C, C++, and Java) is a block structured language. As mentioned earlier in the chapter, a "block" of code is a series of zero or more lines of code enclosed within braces, { }.

Blocks may be nested inside one another to any arbitrary depth.

public class SimpleProgram
{

  // We're inside of the 'class' block (one level deep).
  static void Main() {
    // We're inside of the 'main method' block (two levels deep).
    int x = 3;
    int y = 4;
    int z = 5;

    if (x > 2) {
      // We're now one level deeper (level 3), in a nested block.
      if (y > 3) {
        // We're one level deeper still (level 4), in yet another
        // nested block.
        // (We could go on and on!)
      } // We've just ended the level 4 block.
      // (We could have additional code here, at level 3.)
    } // Level 3 is done!
    // (We could have additional code here, at level 2.)
  } // That's it for level 2!
  // (We could have additional code here, at level 1.)
} // Adios, amigos! Level 1 has just ended.

The scope of a variable name is defined as that portion of code in which a name remains defined to the compiler: typically, from the point where it is first declared down to the closing (right) brace for the block of code that it was declared in. A variable is said to be in scope only inside the block of code in which it is declared. Once program execution exits a block of code, any variables that were declared inside that block go out of scope and will be inaccessible to the program.

As an example of the consequences of variable scope, let's write a program called ScopeDemo, shown next. The ScopeDemo class declares three nested code blocks: one for the ScopeDemo class declaration, one for the Main method, and one as part of an if statement inside the body of the Main method.

public class ScopeDemo
{
  static void Main() {
    double cost = 2.65;

    if (cost < 5.0) {
      double discount = 0.05; // declare a variable inside the 'if' block
      // other details omitted ...
    }

    // When the 'if' block exits, the variable 'discount' goes out of scope, and
    // is no longer recognized by the compiler. If we try to use it in a
    // subsequent statement, the compiler will generate an error.
    double refund = cost * discount;   // this won't compile - discount is no
  }                                       // longer in scope
}

In the preceding example, a variable named cost is declared inside the block of code comprising the Main method body. Another variable named discount is declared inside the block of code associated with the if statement. When the if statement block of code exits, the discount variable goes out of scope. If we try to access it later in the program, as we do in the following line of code:

double refund = cost * discount;

the compiler will generate the following error:

error: CS0103: The name 'discount' does not exist in the class or namespace
'ScopeDemo'

Note that a variable declared in an outer code block is accessible to any inner code blocks that follow the declaration. For example, in the preceding ScopeDemo example, the variable cost is accessible inside the nested if statement code block that follows its declaration.


Team LiB
Previous Section Next Section