How to use global namespace

What is global namespace

We can use keyword global to reference the root namespace.


namespace N/*from ww  w  . j  av  a2 s.  co m*/
{
    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 w  ww.  j  a  va 2 s  .  c  om*/
  
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.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor