What is global :: mean in .Net constructor files?

Here is a question that I had for some time, but in fact I did not ask ...

In quite a few designer files that Visual Studio generates, some of the variables have a global prefix :: Can anyone explain what this means, what this prefix does and where to use it?

+7
c # namespaces
source share
4 answers

The global namespace classifier allows you to access a member in the global ("empty") namespace.

If you were to call an unqualified type (for example, MyClass.DoSomething (), and not MyNamespace.MyClass.DoSomething ()), then it is assumed that it is in the current namespace. How then do you qualify a type to say that it is in a global / empty namespace?

This sample code (console application) should illustrate its behavior:

using System; namespace MyNamespace { public class Program { static void Main(string[] args) { MessageWriter.Write(); // writes "MyNamespace namespace" global::MessageWriter.Write(); // writes "Global namespace" Console.ReadLine(); } } // This class is in the namespace "MyNamespace" public class MessageWriter { public static void Write() { Console.WriteLine("MyNamespace namespace"); } } } // This class is in the global namespace (ie no specified namespace) public class MessageWriter { public static void Write() { Console.WriteLine("Global namespace"); } } 
+12
source share

The prefix indicates the global namespace. Here is an example:

 namespace Bar { class Gnat { } } namespace Foo { namespace Bar { class Gnat { } } class Gnus { Bar.Gnat a; // Foo.Bar.Gnat global::Bar.Gnat b; // Bar.Gnat } } 

Notice how the member may inadvertently relate to the Foo.Bar.Gnat class. To avoid this, use the global :: prefix.

+4
source share

global :: namespace qualifier is used in automatically generated to prevent collision in type resolution through nested namespaces.

+3
source share

From here

When the left identifier is global, the search for the correct identifier starts in the global namespace.

+1
source share

All Articles