---
title: "Variable Scopes in C#"  
description: "Variable Scopes in C#"  
author: "Ravi Vishwakarma"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/interview/33899/variable-scopes-in-c-sharp  
category: "c#"  
tags: ["c#", ".net", ".net assembly", ".net developer"]  
reading_time: 4 minutes  

---

# Variable Scopes in C#

In C#, variable scope refers to the accessibility and lifetime of [variables](https://www.mindstick.com/blog/302641/concept-of-variable-scoping-in-programming-languages) 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.

```cs
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.

```cs
class MyClass {
    void SomeMethod() {
        int localVar = 20; // Local variable
        // localVar is accessible only within SomeMethod
    }
}
```

**Block Scope**: Variables declared within a [block of code](https://www.mindstick.com/blog/302641/concept-of-variable-scoping-in-programming-languages) (within curly braces `{}`) have block scope. They are accessible only within that block.

```cs
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.

```cs
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.

```cs
class MyClass {
    void SomeMethod(int parameter) {
        // parameter is accessible within SomeMethod
    }
}
```

**Lambda Expressions and Anonymous Methods**: Variables used within [lambda expressions](https://www.mindstick.com/interview/308/what-is-the-lambda-expression) and [anonymous methods](https://www.mindstick.com/forum/34712/what-is-anonymous-methods-functions-and-lambda-expressions-in-c) have access to variables in the outer scope, but they must be effectively final (cannot be modified within the lambda/anonymous method).

```cs
class MyClass {
   int outerVar = 50;
   void SomeMethod() {
       Action action = () => {
           // outerVar is accessible here
           // However, if outerVar is modified here, it must be effectively final
       };
   }
}
```

## Answers

### Answer by Ravi Vishwakarma

In C#, variable scope refers to the accessibility and lifetime of [variables](https://www.mindstick.com/blog/302641/concept-of-variable-scoping-in-programming-languages) 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.

```cs
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.

```cs
class MyClass {
    void SomeMethod() {
        int localVar = 20; // Local variable
        // localVar is accessible only within SomeMethod
    }
}
```

**Block Scope**: Variables declared within a [block of code](https://www.mindstick.com/blog/302641/concept-of-variable-scoping-in-programming-languages) (within curly braces `{}`) have block scope. They are accessible only within that block.

```cs
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.

```cs
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.

```cs
class MyClass {
    void SomeMethod(int parameter) {
        // parameter is accessible within SomeMethod
    }
}
```

**Lambda Expressions and Anonymous Methods**: Variables used within [lambda expressions](https://www.mindstick.com/interview/308/what-is-the-lambda-expression) and [anonymous methods](https://www.mindstick.com/forum/34712/what-is-anonymous-methods-functions-and-lambda-expressions-in-c) have access to variables in the outer scope, but they must be effectively final (cannot be modified within the lambda/anonymous method).

```cs
class MyClass {
   int outerVar = 50;
   void SomeMethod() {
       Action action = () => {
           // outerVar is accessible here
           // However, if outerVar is modified here, it must be effectively final
       };
   }
}
```


---

Original Source: https://www.mindstick.com/interview/33899/variable-scopes-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
