Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does global:: mean in the .Net designer files?

Tags:

namespaces

c#

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?

like image 822
Calanus Avatar asked Jul 10 '09 07:07

Calanus


1 Answers

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");
    }
}
like image 58
Rob Levine Avatar answered Sep 20 '22 12:09

Rob Levine