A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/scope-of-variables-in-c-sharp/ below:

C# Scope of Variables - GeeksforGeeks

C# Scope of Variables

Last Updated : 31 Jan, 2025

The part of the program where a particular variable is accessible is termed 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., the scope of a variable can be determined at compile time and is independent of the function call stack. But C# programs are organized in the form of classes.

So, C# scope rules of variables can be divided into three categories as follows:

1. Class Level Scope

Declaring the variables in a class but outside any method makes them directly accessible anywhere in the class. These variables are also termed as fields or class members.

Example:

C#
// C# program to illustrate the
// Class Level Scope of variables
using System;
  
// Declaring a Class
class Geeks { // From here, class level scope starts
  
    // This is a class level variable
    // having class level scope
    int a = 10;
  
    // Declaring a method
    public void display()
    {
        // Accessing class level variable
        Console.WriteLine(a);
    } // Here method ends
  
} // Here class level scope ends
2. 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.

Example:

C#
// C# program to illustrate the
// Method Level Scope of variables
using System;
  
// Declaring a Class
class Geeks { 
  
    // Declaring a method
    public void display()
    { // From here, method level scope starts
  
        // This variable has
        // method level scope
        int m = 47;
  
        // Accessing method level variable
        Console.WriteLine(m);
  
    } // Here method level scope ends
  
    // Declaring another method
    public void display1()
    { // From here, method level scope starts
  
        // This will give compile-time error as
        // 'm' is not accessible outside 'display()' method
        // Console.WriteLine(m); // Uncommenting this line will cause an error
  
    } // Here method level scope ends
  
} 
3. Block Level Scope

These variables are generally declared inside the for, while, or other statements.

Example:

C#
// C# code to illustrate the Block
// Level scope of variables
using System;
  
// Declaring a Class
class Geeks
{ 
  
    // Declaring a method
    public void display()
    { 
  
        // This variable has
        // method level scope
        int i = 0;
  
        for (i = 0; i < 4; i++) {
          
            // Accessing method level variable
            Console.WriteLine(i);
        }
  
        // Here j is a block level variable
        // It is only accessible inside
        // this for loop
        for (int j = 0; j < 5; j++) {
          
            // Accessing block level variable
            Console.WriteLine(j);
        }
  
        // This will give an error as block level
        // variable can't be accessed outside
        // the block
        // Console.WriteLine(j); // Uncommenting this line will cause an error
  
    }
  
} 


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4