Here is a question that I have had for some time but never actually got around to asking...
In quite a lot of the designer files that Visual Studio generates some of the variables are prefixed with global:: Can someone explain what this means, what this prefix does and where should I be using it?
The global namespace qualifier allows you to access a member in the global ("empty") namespace.
If you were to call an unqualified type (e.g. MyClass.DoSomething() rather than MyNamespace.MyClass.DoSomething()), then it is assumed to be in the current namespace. How then do you qualify the type to say it is in the global/empty namespace?
This code sample (console app) should illustrate its behaviour:
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 (i.e. no specified namespace)
public class MessageWriter
{
public static void Write()
{
Console.WriteLine("Global namespace");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With