Local & Global Scopes
The scope of a variable is the part of the code where we can access that variable. In C#, there is a global scope and many possible local scopes.
When we create a variable outside of any method, it can be accessed from almost anywhere in the program, therefore, it is said to have global scope:
main.cs
1234567891011121314151617181920using System; namespace ConsoleApp { internal class Program { static int myVariable = 10; // Variable with a Global Scope static void testMethod() { // Accessing `myVariable` inside `testMethod` Console.WriteLine($"testMethod: {myVariable}"); } static void Main(string[] args) { // Accessing `myVariable` inside `Main` method Console.WriteLine($"Main Function: {myVariable}"); testMethod(); } } }
Note
When you declare a variable outside of any method, it's important to include the
statickeyword before its declaration.
In the example above, we accessed the variable myVariable in both the Main method and the testMethod. However, this isn't always possible.
A variable declared within a code block is only accessible within that block and any sub-code blocks it contains. For instance, a variable declared inside a method is only directly accessible within that method. Similarly, a variable declared inside an if, else if, or else block is only accessible within that block. Such a variable is said to have a local scope.
Take a moment to review the following code and try to understand how it works:
main.cs
123456789101112131415161718192021222324252627282930313233using System; namespace ConsoleApp { class Program { static int variable_1 = 1; static void Main(string[] args) { int variable_2 = 2; if (true) { int variable_3 = 3; Console.WriteLine(variable_1); Console.WriteLine(variable_2); Console.WriteLine(variable_3); } Console.WriteLine(variable_1); Console.WriteLine(variable_2); Console.WriteLine(variable_3); // ERROR HERE Console.WriteLine(variable_4); // ERROR HERE // Note: A variable cannot be used before it's defined in the code } static void myMethod() { int variable_4 = 4; Console.WriteLine(variable_1); Console.WriteLine(variable_2); // ERROR HERE Console.WriteLine(variable_3); // ERROR HERE Console.WriteLine(variable_4); } } }
In the above code there are four variables:
variable_1- global scope;variable_2- local scope, defined in the Main method;variable_3- local scope, defined in theifblock;variable_4- local scope, defined in themyMethodmethod.
The above program may not compile at all but demonstrates where certain variables can be accessed and where they cannot. The area of code where a variable can be accessed is called the scope of that variable.
variable_1is a global variable so it is accessible almost everywhere;variable_2is accessible throughout theMainmethod, including the sub-blocks like theifcondition;variable_3is accessible only inside theifblock;variable_4is accessible only inside themyMethodblock.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the difference between global and local scope in more detail?
Can you give an example of when to use a global variable versus a local variable?
What happens if I try to access a variable outside of its scope?
Awesome!
Completion rate improved to 1.59
Local & Global Scopes
Swipe to show menu
The scope of a variable is the part of the code where we can access that variable. In C#, there is a global scope and many possible local scopes.
When we create a variable outside of any method, it can be accessed from almost anywhere in the program, therefore, it is said to have global scope:
main.cs
1234567891011121314151617181920using System; namespace ConsoleApp { internal class Program { static int myVariable = 10; // Variable with a Global Scope static void testMethod() { // Accessing `myVariable` inside `testMethod` Console.WriteLine($"testMethod: {myVariable}"); } static void Main(string[] args) { // Accessing `myVariable` inside `Main` method Console.WriteLine($"Main Function: {myVariable}"); testMethod(); } } }
Note
When you declare a variable outside of any method, it's important to include the
statickeyword before its declaration.
In the example above, we accessed the variable myVariable in both the Main method and the testMethod. However, this isn't always possible.
A variable declared within a code block is only accessible within that block and any sub-code blocks it contains. For instance, a variable declared inside a method is only directly accessible within that method. Similarly, a variable declared inside an if, else if, or else block is only accessible within that block. Such a variable is said to have a local scope.
Take a moment to review the following code and try to understand how it works:
main.cs
123456789101112131415161718192021222324252627282930313233using System; namespace ConsoleApp { class Program { static int variable_1 = 1; static void Main(string[] args) { int variable_2 = 2; if (true) { int variable_3 = 3; Console.WriteLine(variable_1); Console.WriteLine(variable_2); Console.WriteLine(variable_3); } Console.WriteLine(variable_1); Console.WriteLine(variable_2); Console.WriteLine(variable_3); // ERROR HERE Console.WriteLine(variable_4); // ERROR HERE // Note: A variable cannot be used before it's defined in the code } static void myMethod() { int variable_4 = 4; Console.WriteLine(variable_1); Console.WriteLine(variable_2); // ERROR HERE Console.WriteLine(variable_3); // ERROR HERE Console.WriteLine(variable_4); } } }
In the above code there are four variables:
variable_1- global scope;variable_2- local scope, defined in the Main method;variable_3- local scope, defined in theifblock;variable_4- local scope, defined in themyMethodmethod.
The above program may not compile at all but demonstrates where certain variables can be accessed and where they cannot. The area of code where a variable can be accessed is called the scope of that variable.
variable_1is a global variable so it is accessible almost everywhere;variable_2is accessible throughout theMainmethod, including the sub-blocks like theifcondition;variable_3is accessible only inside theifblock;variable_4is accessible only inside themyMethodblock.
Thanks for your feedback!