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:
- Class Level Scope
- Method Level Scope
- Block Level Scope
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.
- Class level scoped variables can be accessed by the non-static methods of the class in which they are declared.
- The 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.
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.
- These variables are termed as 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 the method’s execution is over.
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.
- These variables are also termed as loop variables or statement variables as they have limited scope within the body of the statement in which they are declared.
- A loop inside a method generally has three levels 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. A method level variable will be accessible to loops inside that method.
- A variable which is declared inside a loop body will not be visible outside the loop body.
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
}
}
Similar Reads
C# | Types of Variables A variable is a name given to a memory location and all the operations done on the variable effects that memory location. In C#, all the variables must be declared before they can be used. It is the basic unit of storage in a program. The value stored in a variable can be changed during program exec
10 min read
Scope rules in C The scope of a variable in C is the block or the region in the program where a variable is declared, defined, and used. Outside this region, we cannot access the variable, and it is treated as an undeclared identifier.The scope is the area under which a variable is visible.The scope of an identifier
6 min read
Scope of a variable Scope of a variable defines the part of code where the variable can be accessed or modified. It helps in organizing code by limiting where variables are available, preventing unintended changes or conflicts. Understanding scope is key to writing clear, efficient, and maintainable programs. In this a
8 min read
Local Variable in C In C language, a variable declared within a function or a block of code is called a local variable. Local variables are frequently used to temporarily store data in a defined scope where they can be accessed and manipulated. They are stored in the memory stack, Once the function or block of code in
3 min read
Global Variables in C Prerequisite: Variables in C In a programming language, each variable has a particular scope attached to them. The scope is either local or global. This article will go through global variables, their advantages, and their properties. The Declaration of a global variable is very similar to that of a
3 min read
Commonly Asked C Programming Interview Questions | Set 1 What is the difference between declaration and definition of a variable/functionAns: Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important
5 min read