You’ve seen declarations of C# entities, such as classes, methods, properties, variables and parameters. Declarations introduce names that can be used to refer to such C# entities. The scope of a declaration is the portion of the app that can refer to the declared entity by its unqualified name. Such an entity is said to be “in scope” for that portion of the app. This section introduces several important scope issues. The basic scope rules are as follows:
The scope of a parameter declaration is the body of the method in which the declaration appears.
The scope of a local-variable declaration is from the point at which the declaration appears to the end of the block containing the declaration.
The scope of a local-variable declaration that appears in the initialization section of a for
statement’s header is the body of the for
statement and the other expressions in the header.
The scope of a method, property or field of a class is the entire body of the class. This enables non-static
methods and properties of a class to use any of the class’s fields, methods and properties, regardless of the order in which they’re declared. Similarly, static
methods and properties can use any of the static
members of the class.
Any block may contain variable declarations. If a local variable or parameter in a method has the same name as a field, the field is hidden until the block terminates—in Chapter 10, we discuss how to access hidden fields. A compilation error occurs if a nested block in a method contains a variable with the same name as a local variable in an outer block of the method. The app in Fig. 7.9 demonstrates scoping issues with fields and local variables.
Use different names for fields and local variables to help prevent subtle logic errors that occur when a method is called and a local variable of the method hides a field of the same name in the class.
Line 8 declares and initializes the static
variable x
to 1
. This static
variable is hidden in any block (or method) that declares a local variable named x
. Method Main
(lines 12–31) declares local variable x
(line 14) and initializes it to 5
. This local variable’s value is output to show that static
variable x
(whose value is 1
) is hidden in method Main
. The app declares two other methods—UseLocalVariable
(lines 34–43) and UseStaticVariable
(lines 46–53)—that each take no arguments and do not return results. Method Main
calls each method twice (lines 19–28). Method UseLocalVariable
declares local variable x
(line 36). When UseLocalVariable
is first called (line 19), it creates local variable x
and initializes it to 25
(line 36), outputs the value of x
(lines 38–39), increments x
(line 40) and outputs the value of x
again (lines 41–42). When UseLocalVariable
is called a second time (line 25), it re-creates local variable x
and reinitializes it to 25
, so the output of each call to UseLocalVariable
is identical.
Method UseStaticVariable
does not declare any local variables. Therefore, when it refers to x
, static
variable x
(line 8) of the class is used. When method UseStaticVariable
is first called (line 22), it outputs the value (1
) of static
variable x
(lines 48–49), multiplies the static
variable x
by 10
(line 50) and outputs the value (10
) of static
variable x
again (lines 51–52) before returning. The next time method UseStaticVariable
is called (line 28), the static
variable has its modified value, 10
, so the method outputs 10
, then 100
. Finally, in method Main
, the app outputs the value of local variable x
again (line 30) to show that none of the method calls modified Main
’s local variable x
, because the methods all referred to variables named x
in other scopes.