Understanding Scope
A variable’s scope is the code that can “see” or access that variable. It determines whether a
piece of code can read the variable’s value and give it a new value.
In this lesson you learn what scope is. You learn why restricting scope is a good thing and how
to determine a variable’s scope.
SCOPE WITHIN A CLASS
A C# class (and note that Form types are classes, too) contains three main kinds of scope:
class scope, method scope, and block scope. (If you have trouble remembering what a class
is, review Lesson 9’s section “Understanding Classes and Instances.”)
Variables with class scope are declared inside the class but outside of any of its methods.
These variables are visible to all of the code throughout the instance of the class and are
known as fields.
Variables with method scope are declared within a method. They are usable by all of the code
that follows the declaration within that method.
Variables with block scope are declared inside a block defined by curly braces {} nested inside
a method. The section “Block Scope” later in this lesson says more about this.
For example, consider the following code that defines the form’s constructor (
Form1), a field,
and some variables inside event handlers:
namespace VariableScope
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
13
596906c13.indd 163 4/7/10 12:32:47 PM
164
LESSON 13 Understanding scope
// A field.
int a = 1;
private void clickMeButton_Click(object sender, EventArgs e)
{
// A method variable.
int b = 2;
MessageBox.Show(“a = “ + a.ToString() +
“\nb = “ + b.ToString());
}
private void clickMeTooButton_Click(object sender, EventArgs e)
{
// A method variable.
int c = 3;
MessageBox.Show(“a = “ + a.ToString() +
“\nc = “ + c.ToString());
}
}
}
The field a is declared outside of the three methods (Form1, clickMeButton_Click, and
clickMeTooButton_Click) so it has class scope. That means the code in any of the methods can
see and use this variable. In this example, the two
Click event handlers each display the value.
The variable
b is declared within clickMeButton_Click so it has method scope. Only the code
within this method that comes after the declaration can use this variable. In particular, the code in
the other methods cannot see it.
Similarly, the code in the
clickMeTooButton_Click event handler that comes after the c declaration
can see that variable.
Two variables with the same name cannot have the same scope. For example, you cannot create two
variables named
a at the class level nor can you create two variables named b inside the same method.
Same Named Variables
Although you cannot give two variables the same name within the same scope, you can give them
the same name if they are in different methods or one is a field and the other is declared inside a
method. For example, the following code defines three variables all named
count:
// A field.
int count = 0;
private void clickMeButton_Click(object sender, EventArgs e)
{
// A method variable.
int count = 1;
MessageBox.Show(count.ToString());
}
596906c13.indd 164 4/7/10 12:32:47 PM

Get Stephens' C# Programming with Visual Studio® 2010 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.