: C# 2008 Programmer

Scope of Variables

Scope of Variables

The scope of a variable (that is, its visibility and accessibility) that you declare in C# is affected by the location in which the variable is declared. Consider the following example where a variable num is declared within the Program class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld {
class Program {
static int num = 7;
static void Main(string[] args) {
Console.WriteLine("num in Main() is {0}", num); //---7---
HelloWorld.Program.Method1();
Console.ReadLine();
return;
}
static private void Method1() {
Console.WriteLine("num in Method1() is {0}", num); //---7---
}
}
}

Because the num variable is declared in the class, it is visible (that is, global) to all the methods declared within the class, and you see the following output:

num in Main() is 7
num in Method1() is 7

However, if you declare another variable with the same name (num) within Main() and Method1(), like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld {
class Program {
static int num = 7;
static void Main(string[] args) {
int num = 5;
Console.WriteLine("num in Main() is {0}", num); //---5---
HelloWorld.Program.Method1();
Console.ReadLine();
return;
}
static private void Method1() {
int num = 10;
Console.WriteLine("num in Method1() is {0}", num); //---10---
}
}
}

You get a very different output:

num in Main() is 5
num in Method1() is 10

That's because the num variables in Main() and Method1() have effectively hidden the num variable in the Program class. In this case, the num in the Program class is known as the global variable while the num variables in Main and Method1 are known as local variables. The num variable in Main() is only visible within Main(). Likewise, this also applies to the num variable in Method1().

What if you need to access the num declared in the Program class? In that case, you just need to specify its full name:

Console.WriteLine("num in Program is {0}", HelloWorld.Program.num); //---7---

While a local variable can hide the scope of a global variable, you cannot have two variables with the same scope and identical names. The following makes it clear:

static void Main(string[] args) {
int num = 5;
Console.WriteLine("num in Main() is {0}", num); //---5---
int num = 6; //---error: num is already declared---
return;
}

However, two identically named variables in different scope would be legal, as the following shows:

static void Main(string[] args) {
for (int i = 0; i < 5; i++) {
//--- i is visible within this loop only---
Console.WriteLine(i);
} //--- i goes out of scope here---
for (int i = 0; i < 3; i++) {
//--- i is visible within this loop only---
Console.WriteLine(i);
} //--- i goes out of scope here---
Console.ReadLine();
return;
}

Here, the variable i appears in two for loops (looping is covered later in this chapter). The scope for each i is restricted to within the loop, so there is no conflict in the scope and this is allowed.

Declaring another variable named i outside the loop or inside it will cause a compilation error as the following example shows:

static void Main(string[] args) {
int i = 4; //---error---
for (int i = 0; i < 5; i++) {
int i = 6; //---error---
Console.WriteLine(i);
}
for (int i = 0; i < 3; i++) {
Console.WriteLine(i);
}
Console.ReadLine();
return;
}

This code results in an error: "A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'parent or current' scope to denote something else."


: 1.151. /Cache: 3 / 0