The part of the program where a particular variable is accessible is termed as the Scope of that variable. A variable can be defined in a class, method, loop etc. In C/C++, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can be determined at compile time and independent of the function call stack. But the C# programs are organized in the form of classes.
So C# scope rules of variables can be divided into three categories as follows:
- Class Level Scope
- Method Level Scope
- Block Level Scope
Class Level Scope
- Declaring the variables in a class but outside any method can be directly accessed anywhere in the class.
- These variables are also termed as the fields or class members.
- Class level scoped variable can be accessed by the non-static methods of the class in which it is declared.
- Access modifier of class level variables doesn’t affect their scope within a class.
- Member variables can also be accessed outside the class by using the access modifiers.
Method Level Scope
- Variables that are declared inside a method have method level scope. These are not accessible outside the method.
- However, these variables can be accessed by the nested code blocks inside a method.
- These variables are termed as the local variables.
- There will be a compile-time error if these variables are declared twice with the same name in the same scope.
- These variables don’t exist after method’s execution is over.
Block Level Scope
- These variables are generally declared inside the for, while statement etc.
- These variables are also termed as the loop variables or statements variable as they have limited their scope up to the body of the statement in which it declared.
- Generally, a loop inside a method has three level of nested code blocks(i.e. class level, method level, loop level).
- The variable which is declared outside the loop is also accessible within the nested loops. It means a class level variable will be accessible to the methods and all loops. Method level variable will be accessible to loop and method inside that method.
- A variable which is declared inside a loop body will not be visible to the outside of loop body.
- C# | Variables
- C# | Types of Variables
- C# | Implicitly Typed Local Variables - var
- Automation Using Selenium in C# With Example
- How to Execute C# Program on cmd (command-line)?
- How to Install and Setup Visual Studio for C#?
- What is .NET 3-Tier Architecture?
- Cross Join in LINQ
- Comparing two ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> in C#
- Compare two ValueTuple<T1, T2, T3, T4, T5, T6, T7> in C#
- Comparing two ValueTuple<T1, T2, T3, T4, T5, T6> in C#
- Comparing two ValueTuple<T1, T2, T3, T4, T5> in C#
- Comparing two ValueTuple<T1, T2, T3, T4> in C#
- Comparing two ValueTuple<T1, T2, T3> in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.