- 7.1 Introduction
- 7.2 Packaging Code in C#
- 7.3 static Methods, static Variables and Class Math
- 7.4 Methods with Multiple Parameters
- 7.5 Notes on Using Methods
- 7.6 Argument Promotion and Casting
- 7.7 The .NET Framework Class Library
- 7.8 Case Study: Random-Number Generation
- 7.9 Case Study: A Game of Chance; Introducing Enumerations
- 7.10 Scope of Declarations
- 7.11 Method-Call Stack and Activation Records
- 7.12 Method Overloading
- 7.13 Optional Parameters
- 7.14 Named Parameters
- 7.15 C# 6 Expression-Bodied Methods and Properties
- 7.16 Recursion
- 7.17 Value Types vs. Reference Types
- 7.18 Passing Arguments By Value and By Reference
- 7.19 Wrap-Up
7.10 Scope of Declarations
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.8 demonstrates scoping issues with fields and local variables.
1 // Fig. 7.8: Scope.cs 2 // Scope class demonstrates static- and local-variable scopes. 3 using System; 4 5 class Scope 6 { 7 // static variable that's accessible to all methods of this class 8 private static int x = 1; 9 10 // Main creates and initializes local variable x 11 // and calls methods UseLocalVariable and UseStaticVariable 12 static void Main() 13 { 14 int x = 5; // method's local variable x hides static variable x 15 16 Console.WriteLine($"local x in method Main is {x}"); 17 18 // UseLocalVariable has its own local x 19 UseLocalVariable(); 20 21 // UseStaticVariable uses class Scope's static variable x 22 UseStaticVariable(); 23 24 // UseLocalVariable reinitializes its own local x 25 UseLocalVariable(); 26 27 // class Scope's static variable x retains its value 28 UseStaticVariable(); 29 30 Console.WriteLine($"\nlocal x in method Main is {x}"); 31 } 32 33 // create and initialize local variable x during each call 34 static void UseLocalVariable() 35 { 36 int x = 25; // initialized each time UseLocalVariable is called 37 38 Console.WriteLine( 39 $"\nlocal x on entering method UseLocalVariable is {x}"); 40 ++x; // modifies this method's local variable x 41 Console.WriteLine( 42 $"local x before exiting method UseLocalVariable is {x}"); 43 } 44 45 // modify class Scope's static variable x during each call 46 static void UseStaticVariable() 47 { 48 Console.WriteLine("\nstatic variable x on entering method " + 49 $"UseStaticVariable is {x}"); 50 x *= 10; // modifies class Scope's static variable x 51 Console.WriteLine("static variable x before exiting " + 52 $"method UseStaticVariable is {x}"); 53 } 54 }
local x in method Main is 5 local x on entering method UseLocalVariable is 25 local x before exiting method UseLocalVariable is 26 static variable x on entering method UseStaticVariable is 1 static variable x before exiting method UseStaticVariable is 10 local x on entering method UseLocalVariable is 25 local x before exiting method UseLocalVariable is 26 static variable x on entering method UseStaticVariable is 10 static variable x before exiting method UseStaticVariable is 100 local x in method Main is 5
Fig. 7.8 | Scope class demonstrates static and local-variable scopes.
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.