Scope of Variables in C#

The scope of a variable in C# determines the region of code where a variable can be accessed and used. Understanding variable scope is crucial for writing efficient and error-free programs.

C# has several levels of variable scope, each with different accessibility rules and lifetimes.

Types of Variable Scope

Method Level (Local Variables)

Variables declared inside a method are local variables. They are only accessible within that specific method and are destroyed when the method execution completes −

using System;

class Program {
   public void TestMethod() {
      int localVar = 10; // local variable
      Console.WriteLine("Local variable: " + localVar);
   }
   
   public static void Main() {
      Program p = new Program();
      p.TestMethod();
      // localVar is not accessible here
   }
}

The output of the above code is −

Local variable: 10

Class Level (Instance Variables)

Variables declared inside a class but outside any method are instance variables or class member variables. They are accessible throughout the class −

using System;

class Calculator {
   // Class level variables (instance variables)
   private int num1;
   private int num2;
   
   public void SetNumbers(int a, int b) {
      num1 = a; // accessing class level variable
      num2 = b; // accessing class level variable
   }
   
   public int Add() {
      return num1 + num2; // both variables accessible here
   }
   
   public void DisplayNumbers() {
      Console.WriteLine("Number 1: " + num1);
      Console.WriteLine("Number 2: " + num2);
   }
}

class Program {
   public static void Main() {
      Calculator calc = new Calculator();
      calc.SetNumbers(25, 15);
      calc.DisplayNumbers();
      Console.WriteLine("Sum: " + calc.Add());
   }
}

The output of the above code is −

Number 1: 25
Number 2: 15
Sum: 40

Block Level Scope

Variables declared inside code blocks (within curly braces) have block-level scope −

using System;

class Program {
   public static void Main() {
      int x = 10;
      
      if (x > 5) {
         int y = 20; // block level variable
         Console.WriteLine("Inside block: x = " + x + ", y = " + y);
      }
      
      Console.WriteLine("Outside block: x = " + x);
      // y is not accessible here - would cause compilation error
   }
}

The output of the above code is −

Inside block: x = 10, y = 20
Outside block: x = 10

Variable Scope Hierarchy Class Level Method Level Block Level { int x = 5; } Innermost scope Inner scopes can access outer scope variables

Variable Scope Rules

Scope Type Accessibility Lifetime
Class Level Throughout the class Exists as long as the object exists
Method Level Within the method only Created when method starts, destroyed when method ends
Block Level Within the block only Created when entering block, destroyed when leaving block

Example with Multiple Scope Levels

using System;

class ScopeDemo {
   private int classVar = 100; // Class level variable
   
   public int Divide(int num1, int num2) {
      // Method level variables
      int result;
      result = num1 / num2;
      
      if (result > 10) {
         int bonus = 5; // Block level variable
         result += bonus;
         Console.WriteLine("Bonus added: " + bonus);
      }
      
      return result;
   }
   
   public static void Main() {
      // Method level variables
      int a = 150;
      int b = 10;
      int res;
      
      ScopeDemo demo = new ScopeDemo();
      res = demo.Divide(a, b);
      Console.WriteLine("Division Result = " + res);
      Console.WriteLine("Class variable = " + demo.classVar);
   }
}

The output of the above code is −

Bonus added: 5
Division Result = 20
Class variable = 100

Conclusion

Variable scope in C# determines where variables can be accessed within your program. Class-level variables are accessible throughout the class, method-level variables are local to their methods, and block-level variables exist only within their code blocks. Understanding scope helps prevent naming conflicts and ensures proper variable lifetime management.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements