Section 1.10. Access Objects in the Global Namespace


1.10. Access Objects in the Global Namespace

As in previous versions of C#, the namespace keyword is used to declare a scope. This lets you organize your code and prevents identifier collisions (for example, two different classes with the same name), especially when using third-party components.

Any object that is not defined within a specific namespace is in the global namespace. Objects in the global namespace are available to objects in any other namespace. If a name collision occurs, however, you will need a way to specify that you want the object in the global namespace rather than in the local namespace.


Note: The global namespace qualifier allows you to specify an identifier in the (default) global namespace rather than in the local namespace.

1.10.1. 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

1.10.2. What just happened?

In this somewhat artificial example, you create a nested class that you named System and you created a local Boolean variable named Console. You have blocked access to the global System and Console identifiers, so neither of these lines will compile:

Console.WriteLine(x);  System.Console.WriteLine(x);

To designate that you want to use the System object in the global namespace, you will use the global namespace qualifier:

global::System.Console.WriteLine(x);

Notice that in the final line, the global namespace qualifier is used to access the System and Console objects in the global namespace, and the unqualified Console identifier is used to access the local Boolean value:

global::System.Console.WriteLine(Console);

1.10.3. What about . . .

...other uses for the double-colon (::) operator?

The :: operator is the namespace alias qualifier. It always appears between two identifiers:

identifierOne::identifierTwo

If identifierOne is the global namespace, this operator is used to find identifierTwo within the global namespace. But if identifierOne is any namespace other than the global namespace, the operator serves to restrict the lookup of identifierOne.

1.10.4. Where can I learn more?

The global namespace qualifier is mentioned in the MSDN article "Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes" by Juval Lowy, available at http://msdn.microsoft.com/msdnmag/issues/04/05/c20/.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net