New Features in C# 2.0—Global Namespace: How do I do that?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Tutorials
C# Tutorials

New Features in C# 2.0

© 2005 O'Reilly Media, Inc.

How do I do that?

To access objects in the global namespace, you use the new global namespace qualifier (global::), as shown in Example 1-8.

Example 1-8. Using the global namespace

using System;
 
namespace GlobalNameSpace
{
  class Program
  {
    // create a nested System class 
    // that will provide
    // a set of utilities for 
    // interacting with the
    // underlying system 
    // (conflicts with System namespace)
    public class System
    {
    }
 
    static void Main(string[ ] args)
    {
      // flag indicates if we're in a console app
      // conflicts with Console in System namespace
      bool Console = true;
      int x = 5;
 
      // Console.WriteLine(x); // won't compile - conflict with Console
      // System.Console.WriteLine(x); // conflicts with System
      global::System.Console.WriteLine(x); // works great.
      global::System.Console.WriteLine(Console);
    }
  }
}

Output:

5
True


Previous_Page_.gif Next_Page_.gif


Personal tools