LA.NET [EN]

Oct 29

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.

5 comments so far

  1. ram
    3:01 pm - 10-29-2009

    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()”…

  2. luisabreu
    3:23 pm - 10-29-2009

    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…

  3. Randy
    5:46 am - 10-31-2009

    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.

  4. luisabreu
    2:21 pm - 10-31-2009

    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)

  5. samantha ramos
    10:29 am - 11-16-2009

    what is a qualifiers use in a sentence?
    pleas help me i really need it

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>