views:

144

answers:

4

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?

+1  A: 

From here

When the left identifier is global, the search for the right identifier starts at the global namespace.

Colin
That points to correct answer.
Ihar Voitka
+3  A: 

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
  }
}

Note how the member a perhaps inadvertedly refers to the Foo.Bar.Gnat class. To avoid this use the global:: prefix.

Martin Liversage
How is it related to generated code? No types from global namespaces are referenced usually in .net designer classes.
Ihar Voitka
The code generator wants to reference the class Bar.Gnat. If the code is generated inside the Foo namespace the class Foo.Bar.Gnat is referenced unless the global:: prefix is used. The code generator wants to protect the generated code from this mistake. It is a bit paranoid, but nevertheless the correct thing to do, and probably an internal Microsoft standard.
Martin Liversage
+7  A: 

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");
    }
}
Rob Levine
This does not answer the question why. Generated code uses types declared in namespaces and still global:: is used.
Ihar Voitka
@Ihar: Global is used in a similar way to specify that a namespace should not be a subnamespace...
Arjan Einbu
No - the question asks three things: What this means (explained), what this prefix does (explained) and where I should be using it (which you will know once you've understood the other two points). Your question - "why is it used in designer files" was not asked by the original questioner.
Rob Levine
@lhar : oops - hit add a bit early there. Meant to add, as Arjan says, it is to ensure there is no class with sub namespaces.e.g. if the designer references a type, System.Resources.ResourceManager in an autogenerated class in the namespace MyProject.Library, then the reason it prefixes it with global is just in case there is actually a type MyProject.Library.System.Resources.ResourceManager. Otherwise this second type would be inadvertantly used by the designer.
Rob Levine
@rob well, your point is correct. Calanus accepted answer, so I assume that his curiosity is satisfied. But it is not the case in generated code. Have you ever seen types from global namespace referenced in .net designer files? The reason of using global:: qualifier is to prevent type resolving collision when searching through nested namespaces.
Ihar Voitka
@lhar - ha ha - I was just typing the second half of a response when you beat me too it! Yes - your point is right :)
Rob Levine
+1  A: 

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

Ihar Voitka