In C#, variable scope refers to the accessibility and lifetime of
variables within a program.
Here are the common variable scopes in C#:
Global Scope: Variables declared outside of any method, typically at the class level, have global scope. They are accessible from any method within the class.
class MyClass {
int globalVariable = 10; // Global variable
void SomeMethod() {
// globalVariable is accessible here
}
}
Local Scope: Variables declared within a method have local scope. They are accessible only within the method where they are declared.
class MyClass {
void SomeMethod() {
int localVar = 20; // Local variable
// localVar is accessible only within SomeMethod
}
}
Block Scope: Variables declared within a
block of code (within curly braces {}) have block scope. They are accessible only within that block.
class MyClass {
void SomeMethod() {
{
int blockVar = 30; // Block variable
// blockVar is accessible only within this block
}
// blockVar is not accessible here
}
}
Class Scope (Static Variables): Static variables declared within a class have class scope. They are accessible to all instances of the class.
class MyClass {
static int staticVar = 40; // Static variable
void SomeMethod() {
// staticVar is accessible here
}
}
Method Parameters: Parameters passed to a method have scope within that method.
class MyClass {
void SomeMethod(int parameter) {
// parameter is accessible within SomeMethod
}
}
Lambda Expressions and Anonymous Methods: Variables used within
lambda expressions and
anonymous methods have access to variables in the outer scope, but they must be effectively final (cannot be modified within the lambda/anonymous method).
class MyClass {
int outerVar = 50;
void SomeMethod() {
Action action = () => {
// outerVar is accessible here
// However, if outerVar is modified here, it must be effectively final
};
}
}
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, variable scope refers to the accessibility and lifetime of variables within a program.
Here are the common variable scopes in C#:
Global Scope: Variables declared outside of any method, typically at the class level, have global scope. They are accessible from any method within the class.
Local Scope: Variables declared within a method have local scope. They are accessible only within the method where they are declared.
Block Scope: Variables declared within a block of code (within curly braces
{}) have block scope. They are accessible only within that block.Class Scope (Static Variables): Static variables declared within a class have class scope. They are accessible to all instances of the class.
Method Parameters: Parameters passed to a method have scope within that method.
Lambda Expressions and Anonymous Methods: Variables used within lambda expressions and anonymous methods have access to variables in the outer scope, but they must be effectively final (cannot be modified within the lambda/anonymous method).