What is the scope resolution operator in C#?


The scope resolution operator in C# has a different meaning as compared with C++. In C++ the :: is used for global variables, whereas in C# it is related to namespaces.

If you have a type that share an identifier in different namespace, then to identify them use the scope resolution operator.

For example, to reference System.Console class, use the global namespace alias with the scope resolution operator.

global::System.Console

Example

 Live Demo

using myAlias = System.Collections;
namespace Program {
   class Demo {
      static void Main() {
         myAlias::Hashtable h = new myAlias::Hashtable();
         h.Add("Q", "1");
         h.Add("R", "2");
         h.Add("S", "3");
         h.Add("T", "4");
         h.Add("U", "5");
         foreach (string n in h.Keys) {
            global::System.Console.WriteLine(n + " " + h[n]);
         }
      }
   }
}

Output

T 4
R 2
S 3
U 5
Q 1

Updated on: 23-Jun-2020

918 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements