This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Variable Scopes

The forty-seventh part of the C# Fundamentals tutorial discusses the concept of variable scopes. The scope of a variable determines its lifetime and controls its visibility and availability to each of the methods within a program.

What are Variable Scopes?

The scope of a variable determines its visibility to the rest of a program. In the examples throughout the C# Fundamentals tutorial, variables have been defined within methods. When created in this way, the scope of the variable is the entire method after the declaration. This means that the variable is available to use within the method but when control passes to another method the variable is unavailable.

There are other possibilities for a variable's scope. For example, a variable can be declared within a loop or other code structure and be only visible to the code within the structure. A wider scoped variable could be declared at class-level so that it can be used by any method within the class. In fact, a variable's scope is always the full extent of the code block it is declared within.

As has been demonstrated throughout the tutorial, code blocks can be nested. A loop within a method within a class provides three levels of nested code blocks and, therefore, three levels of nested scope. When a variable is declared within one of these nested scopes, it is visible only to the current scope and any scopes nested within it. This means that a variable declared within a loop is not visible outside of that loop whereas a variable declared outside of the loop is also visible within the loop.

Class-Level Scope

Variables that are defined at the class level are available to any non-static method within the class. In this article the full extent of class scopes is not discussed as the object-oriented programming techniques involved are beyond the scope of a beginner's tutorial.

Method-Level Scope

Variables declared within a method's code block are available for use by any other part of the method, including nested code blocks. The following example demonstrates this by creating a variable in the method, setting its value and then using it within the scope of an 'if' statement.

static void Main(string[] args)
    int score;                                          // Declared at method-level
    score = 100;                                        // Used at method-level

    if (score >= 50)
        Console.WriteLine("Good score : {0}", score);   // Used in nested scope
        Console.WriteLine("Poor score : {0}", score);   // Used in nested scope

Nested Scope

As described earlier in the article, variables declared within a nested scope are not available to those outside of their code block. The following code will not compile because the variable that is attempted to be used at method level is declared in the more deeply nested scope of the if statement.

static void Main(string[] args)
    int score = 100;

    if (score >= 50)
        string message = "Good score";                  // Declared in if statement
        string message = "Poor score";                  // Declared in if statement

    Console.WriteLine(message);                         // Variable unavailable

NB: To make this example work, the variable would be declared before the if statement and simply assigned a value within the if statement.

11 August 2007