Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Global and Local Variables in C#
In C#, variables are classified by their scope − the region of code where they can be accessed. The two main types are local variables (declared within methods) and global variables (which C# handles through static class members and namespace aliases).
Local Variables
A local variable is declared within a method, constructor, or block of code. Its scope is limited to that specific block, meaning it can only be accessed within the method or block where it was declared.
Syntax
dataType variableName; // or dataType variableName = value;
Example
using System;
public class Program {
public static void Main() {
int localVar = 100; // local variable
Console.WriteLine("Local variable value: " + localVar);
DisplayMessage();
// Console.WriteLine(message); // Error: message not accessible here
}
public static void DisplayMessage() {
string message = "Hello from method"; // local to this method
Console.WriteLine(message);
}
}
The output of the above code is −
Local variable value: 100 Hello from method
Global Variables in C#
C# does not support true global variables like C++. Instead, C# achieves global accessibility through static class members and uses the global namespace alias (::) to resolve naming conflicts between namespaces.
Using Static Class Members as Global Variables
using System;
public static class GlobalData {
public static int Counter = 0;
public static string ApplicationName = "My App";
}
public class Program {
public static void Main() {
Console.WriteLine("App Name: " + GlobalData.ApplicationName);
GlobalData.Counter = 5;
Console.WriteLine("Counter: " + GlobalData.Counter);
ModifyGlobal();
Console.WriteLine("Counter after modification: " + GlobalData.Counter);
}
public static void ModifyGlobal() {
GlobalData.Counter += 10;
}
}
The output of the above code is −
App Name: My App Counter: 5 Counter after modification: 15
Global Namespace Alias
The global namespace alias (::) is used to access types when there are naming conflicts between different namespaces −
Example
using System;
using System.Collections;
namespace MyProgram {
class Program {
static void Main() {
// Using global namespace alias to avoid conflicts
global::System.Console.WriteLine("Using global namespace alias");
Hashtable table = new Hashtable();
table.Add("Key1", "Value1");
table.Add("Key2", "Value2");
foreach (string key in table.Keys) {
global::System.Console.WriteLine(key + ": " + table[key]);
}
}
}
}
The output of the above code is −
Using global namespace alias Key1: Value1 Key2: Value2
Comparison of Local vs Global Scope
| Local Variables | Global Variables (Static Members) |
|---|---|
| Declared within methods or blocks | Declared as static class members |
| Limited scope within the declaring block | Accessible throughout the application |
| Memory allocated on stack | Memory allocated in static memory area |
| Destroyed when method exits | Exist for the entire program execution |
Conclusion
C# uses local variables for method-specific data and static class members to achieve global accessibility. The global namespace alias (::) helps resolve naming conflicts between namespaces, providing a way to explicitly specify which namespace a type belongs to.
