C# global namespace

In this chapter you will learn:

  1. How to use global namespace
  2. Use the global alias

What is global namespace

We can use keyword global to reference the root namespace.


namespace N/*from  w  ww .  jav a 2  s.com*/
{
    class A
    {
        static void Main()
        {
            new A.B();
            new global::A.B();
        }
        public class B { }
    }
}
namespace A
{
    class B { }
}

The code above generates the following result.

Use the global alias


using System;  /*from ww  w  .  j av  a2  s.  c o  m*/
  
using Ctr = Counter;  
 
namespace Counter {  
  class MyClass {  
  }  
}  
  
// Declare another class called CountDown, which  
// is in the global namespace.  
class MyClass { 
} 
 
class GlobalAliasQualifierDemo {  
  public static void Main() {  
 
    Ctr::MyClass m = new Ctr::MyClass();  
 
    // Next, create CountDown object from global namespace. 
    global::MyClass m2 = new global::MyClass();  
  }  
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is C# Object
  2. Object methods
Home »
  C# Tutorial »
    C# Types »
      C# Namespace
C# Namespaces
C# namespace using Directive
C# Aliasing Types and Namespaces
C# global namespace