Lesson 1.9 – Variable Scope
C# Course for Absolute Beginners – Lesson 1.9
Next Lesson
Introduction
In previous labs we’ve learned how to declare variables. We then learned how to use the if statement to declare a conditional block of code that can be run based on pre-set conditions. In this lab we’ll discuss the issue of variable scope – in other words, we’ll discuss in what context the variables that you declare are available from one code block to the next, like the code blocks that may be embedded deeper within a given code block. This will be easier to understand with a quick illustration.
Step 1: A Simple In-Scope Demonstration

To begin: Open a new project and retype the code you see in the image:
This example is slightly nonsensical. In line 12 we set a equal to 1, and in line 14 we test to see if a is equal to 1 – of course it is. What we want to know is, will the variable a in line 16 (1) be available, even though it’s in a different code block from the code block in which the variable was first declared in line 12 (2).

To answer our question we’ll run the program. We see that the Console Window prints out the number 1, which is to be expected.
Step 2: A More Complex In-Scope Demonstration

To expand this example, we’ll declare a second variable called b and set the value equal to 2. We’ll also change WriteLine to "Console.WriteLine(b)," so when the program runs the number 2 should be displayed in the Console Window.

Run the program, and the number 2 appears – the program works as it should.
Step 3: A Simple Out-Of-Scope Demonstration

Now change the application by moving the WriteLine method to 20 – we’re attempting to access of the value of b outside of it’s code block.

Run the application and the program throws an exception. The problem is that b does not exist in the current context.

What this means is that it’s possible to access a variable embedded inside a code block. For example, lines 10 to 24 form one code block – the static void Main code block. Lines 14 to 18 form another code block inside the first. The static void Main code block can access the variable from the if statement, because the if statement is inside of the static void Main.
However the reverse is not true. It is not possible to declare a variable in the code block at line 16 and then reference it in line 20, because it is out of scope – it is out of that code block.
Step 4: A More Complex Out-Of-Scope Demonstration

To further demonstrate this point, recreate the method seen in the image. We’ll have an entire lab devoted to the topic of methods but for now you should be able to read and understand the code.
In this code, we’ve created a method called "test."
We’ve removed Console.WriteLine(b); from the previous example in order to avoid confusion.
We’ve added Console.WriteLine(a); to our new test method.

Run the application and you’ll notice that there are build errors. The issue is that the name a does not exist in the current context.

Review the code – notice that the variable a was declared in the code block at line 12; the curly braces shows that this code block ends at line 21. Because the variable a was declared in the first code block, it is not available – it is not in scope – to the code block at lines 24 to 26. We see by the position of the curly braces that these two code blocks are at the same level, or indentation. If the new test method was embedded within the previous method it could still be accessed. However because the test method is not embedded, it’s outside of the scope.
Conclusion
In future labs you’ll see that you can create variables that are accessible by all code blocks beneath the class code block, or the class definition. This will be in the Day 03 or 04 set of labs. For now, it is important that you understand scope as described in this lab. Understand that code blocks define the scope of a given variable that you create. If in the future if you receive an error message that says your variable "does not exist in the current context," you should understand that your variables aren’t declared in such a way that the current code block can actually see them. Essentially, all you need to do is rethink where you declare your variables and understand how code blocks affect that.

