Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Code Blocks, Scopes, and Declaration Spaces

Code blocks are often referred to as “scopes,” but the two terms are not exactly interchangeable. The scope of a named thing is the region of source code in which it is legal to refer to the thing by its unqualified name. The scope of a local variable is exactly the text of the code block that encloses it, which explains why it is common to refer to code blocks as “scopes.”

Scopes are often confused with declaration spaces. A declaration space is a logical container of named things in which two things may not have the same name. A code block not only defines a scope, it also defines a local variable declaration space; it is illegal for two local variable declarations with the same name to appear in the same declaration space. Similarly, it is not possible to declare two methods with the signature of Main() within the same class. (Though the rule is relaxed somewhat for methods; two methods may have the same name in a declaration space provided that they have different signatures.) A code block not only defines a scope, it also defines a local variable declaration space. That is to say, within a block a local can be mentioned by name and must be the unique thing that is declared with that name in the block. Outside the declaring block there is no way to refer to a local by its name; the local is said to be “out of scope” outside the block.

In short: A scope is used to determine what thing a name refers to; a declaration space determines when two things declared with the same name conflict with each other. In Listing 3.27, declaring the local variable message inside the block statement embedded in the if statement restricts its scope to the block statement only; the local is “out of scope” when its name is used later on in the method. To avoid the error, you must declare the variable outside the if statement.

Listing 3.27. Variables Inaccessible outside Their Scope

class Program
   static void Main(string[] args)
       int playerCount;
           "Enter the number of players (1 or 2):");
       playerCount = int.Parse(System.Console.ReadLine());
       if (playerCount != 1 && playerCount != 2)
           string message =                                                                
               "You entered an invalid number of players.";                                
           // ...
       // Error:  message is not in scope.                                                 

Output 3.15 shows the results of Listing 3.27.

Output 3.15.


...\Program.cs(18,26): error CS0103: The name 'message' does not exist
in the current context

The declaration space throughout which a local’s name must be unique includes all the child code blocks textually enclosed within the block that originally declared the local. The C# compiler prevents the name of a local variable declared immediately within a method code block (or as a parameter) from being reused within a child code block. In Listing 3.27, because args and playerCount are declared within the method code block, they cannot be declared again anywhere within the method.

The name message refers to this local variable throughout the scope of the local variable: that is, the block immediately enclosing the declaration. Similarly, playerCount refers to the same variable throughout the block containing the declaration, including within both of the child blocks that are the consequence and alternative of the if statement.

  • + Share This
  • 🔖 Save To Your Account