C# Scope
C# scope indicates a range in which an item can be accessible. The item can be a namespace, a class, a struct, a field, a method or a variable, etc. In most cases, the scope of an item can be defined by its access modifier, i.e. public, private, protected, internal or protected internal which was discussed in the previous sections. However, these access modifiers cannot be used in a local variable or constant. Therefore, we'll focus on the scopes of local variables in this section.
Example 01-93-01
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System; class Program { public void MethodA() { i = 2; Console.WriteLine("i={0}", i); } static void Main() { Program p = new Program(); p.MethodA(); Console.Read(); } int i = 1; }
Output
i=2
Explanation
- Line 7: Assign 2 to the field i before the declaration statement in line 18.
- Line 18: Declare the private field i and initialize it.
In the above example, the field defined in line 18 actually can be declared anywhere in the class and it will not affect its usage in the methods.
Let's change the example a little bit and see what's going on.
Example 01-93-02
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System; class Program { public void MethodA() { //i = 2; // Local variable now int i = 2; Console.WriteLine("i={0}", i); Console.WriteLine("this.i={0}", this.i); } static void Main() { Program p = new Program(); p.MethodA(); Console.Read(); } int i = 1; }
Output
i=2 this.i=1
Explanation
- Line 7: Commented out. It is a local variable now and we will get an error before its declaration if the double forward slash is removed.
- Line 8: Declare the local variable. This line makes the variable i in line 7 locally.
- Line 10: In this case, the class field i can be accessed only by this keyword in the method.
In summary, if the local variable is declared in a method, the class variable with the same name will be hidden in the method and it can only be accessed by using this keyword.
We'll add more code in the example 01-93-02
Example 01-93-03
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System; class Program { int i = 1; public void MethodA() { int i = 2; Console.WriteLine("i={0}", i); } public void MethodB() { int i = 3; Console.WriteLine("i={0}", i); } public void MethodC() { Console.WriteLine("i={0}", i); } static void Main() { Program p = new Program(); p.MethodA(); p.MethodB(); p.MethodC(); Console.Read(); } }
Output
i=2 i=3 i=1
Explanation
In methodA and MethodB, 2 local variables with the same name is declared and are not interrupted each other. In methodC, no local variable is defined so the class field is printed.
The scope of a local variable is the block of the variable declared and the block always refers to the nearest of a pair of curly brackets which are used in conditional, loop or other statements. A local variable can only be seen in its scope and an compile-time error will be issued if another local variable or constant with the same name is declared within the same scope of the variable. Declaring multiple variables in a single declaration statement is equivalent to declaring each variable in multiple statements consequently.
Example 01-93-04
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System; class Program { static void A() { for (int i = 0; i < 2; i++) { Console.Write(i.ToString() + " "); } Console.WriteLine(); if (true) { char i = 'A'; Console.WriteLine(i); } } static void B() { for (int i = 0; i < 2; i++) { Console.Write(i.ToString() + " "); } Console.WriteLine(); //char i = 'A'; // Error //Console.WriteLine(i); // Error } static void C() { float f = (f = 1.23f); Console.WriteLine(f); } static void D() { int i = 1, j = 2, k = i + j; Console.WriteLine("i={0}, j={1}, k={2}", i, j, k); } static void Main() { A(); B(); C(); D(); Console.Read(); } }
Output
0 1 A 0 1 1.23 i=1, j=2, k=3
Explanation
- Line 7-10: The scope of the integer variable i.
- Line 14-17: The scope of the char variable i. Both i work fine on their own scope.
- Line 28-29: If the double forward slash at the beginning of each line is removed, the following error message will be issued. Because the scope of both variable i are overlapped.
- Line 29: If the double forward slash is removed only in this line, you will get the following error message. Because the integer i can only be seen in lines 22-25.
- Line 34: This is equivalent to the following.
- Line 40: This is equivalent to the following.
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 'child' scope to denote something else
The name 'i' does not exist in the current context
float f;
f = (f = 1.23f);
int i; i = 1; int j; j = 2; int k; k = i + j;