The global:: qualifier in C#

Published Thu, Oct 29 2009 13:16

Today I was a little bit surprised when I was asked if we really needed the global:: qualifier. If you’ve been doing C# for some time, the answer is (absolutely) yes. However, things might not be so clear for a beginner (btw, the question was asked in the context of auto-generated by VS which used it to qualify the name of a base class). The best way to understand it is to write a quick sample where you might really need to use this qualifier:

class Test {
    public void SayHi() {
        Console.WriteLine("hi global");
    }
}
namespace ConsoleApplication1 {
    class Test {
        public void SayHi() {
            Console.WriteLine("hi within");
        }
    }
    class Program {
        static void Main(string[] args) {
            new Test().SayHi(); //???
        }
    }
}

The previous sample is valid C# code (notice that we’ve introduced two classes with *similar* names – Test and ConsoleApplication.Test). The real question is: what happens when we create a new Test instance from within the Main method?

In the previous sample, we’ll end up instantiating ConsoleApplication1.Test. And what if we wanted to instantiate Test? Yes, the answer is the global::  qualifier. After showing this example, there really wasn’t any doubt regarding the usefulness of the global::qualifier: you’ll need it in some cases to force the use of a specific type or name.

Filed under:

Comments

# ram said on Thursday, October 29, 2009 10:01 AM

I understand the use of the "global::" stuff but although this peace of code is correct you have to admit that it is really weird. I see 3 things that should not or are not likely to happen in a real program: some code without a namespace (WTF?), two classes with the same name, and the fact that the compiler doesn't throw an error when you do "new Test().SayHi()"...

# luisabreu said on Thursday, October 29, 2009 10:23 AM

Fair enough. Then just wrap everything in namespace A. Then you'll need global::A.Test for getting to test declared in namespace A.

You might end up getting this kind of problem (classes with the same name in different namespaces) more easily than you expect...

# Randy said on Saturday, October 31, 2009 12:46 AM

I think he meant wrap only the first class in a namespace...not the entire thing.  While this type of collision is possible, it is only possible when people don't follow the basic rules of naming namespaces.  If you ever have to use global::, something is wrong, but it is nice to have it as a bandaid.

# luisabreu said on Saturday, October 31, 2009 9:21 AM

most of the time, you'll see global used for tools. that doesn't mean you can't use it either (though most of the time people will use the alias option instead)

# samantha ramos said on Monday, November 16, 2009 4:29 AM

what is a qualifiers use in a sentence?

pleas help me i really need it

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover