- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why we do not have global variables in C#?
C# does not have global variables and the scope resolution operator used in C++ for global variables is related to namespaces. It is called global namespace alias.
If you have a type that shares an identifier in a different namespace, then to identify them using the scope resolution operator.
For example, to reference System.Console class, use the global namespace alias with the scope resolution operator −
global::System.Console
Let us now see an example −
Example
using myAlias = System.Collections; namespace Program { class Demo { static void Main() { myAlias::Hashtable h = new myAlias::Hashtable(); h.Add("M", "1"); h.Add("N", "2"); h.Add("O", "3"); h.Add("P", "4"); foreach (string n in h.Keys) { global::System.Console.WriteLine(n + " " + h[n]); } } } }
Output
N 2 O 3 M 1 P 4
- Related Articles
- Why should we avoid using global variables in C/C++?
- Why are global variables bad in C/C++?
- Why do we have bones?
- Why do we have mirrors in Lifts?
- Global and Local Variables in C#
- What are global variables in C++?
- How and why to avoid global variables in JavaScript?
- What are local variables and global variables in C++?
- How do I declare global variables on Android?
- Why are global and static variables initialized to their default values in C/C++?
- Why we do not drink sea water?
- How do I share global variables across modules in Python?
- Global variables in Java
- Initialization of global and static variables in C
- How do I declare global variables on Android using Kotlin?

Advertisements