57

I would like to understand why you might want to use the global:: prefix. In the following code, ReSharper is identifying it as redundant, and able to be removed:

alt text

11
  • 7
    @John, Just curious - trying to learn why it's used by the code-gen process.
    – Paul Fryer
    Aug 24, 2010 at 0:43
  • 44
    @John-Saunders Telling someone to ignore their curiosity is horrible advice. How exactly do you expect anyone to learn? @Paul-Fryer I think this is a great question - I've often wondered the same thing myself but haven't gotten around to asking.
    – Jake
    Aug 24, 2010 at 0:53
  • 22
    @John - Not sure how to respond to your comment "don't you have better things to do with your time". This time is spent trying to become a better developer and programmer. I would ask you don't you have better things to do than leave pointless comments that don't help anyone?
    – Paul Fryer
    Aug 24, 2010 at 0:57
  • 8
    Well said Paul. People like John, should be banned from access to community sites since comments like this highly demotivates the spirit of the others. Aug 18, 2011 at 14:01
  • 6
    "curiosity and cats, keep it in mind. Don't you have better things to do with your time?" That is the most idiotic comment I have ever seen on a question-and-answer forum.
    – Ed Graham
    Nov 1, 2012 at 11:12

3 Answers 3

67

The keyword global:: causes the compiler to bind names starting in the global namespace as opposed to in the current context. It's needed in places where a bindable member exists in a given context that has the same name as a global one and the global one is desired.

For example

class Test {
  class System {}
  public void Example() {
    System.Console.WriteLine("here"); // Error since System binds to Test.System
    global::System.Console.WriteLine("here"); // Works
}

The corresponding MSDN page has a few more examples (including the one above)

53

It is best to use the global namespace prefix in generated code. This is done to avoid situations where a similar named type exists in your namespace.

If you create a type named System.Diagnostics.DebuggerNonUserCodeAttribute inside your namespace you will notice that ReSharper no longer says that the global:: is not needed. The code generator simply wants to avoid any collisions with the names of your own types.

1
  • Short,simple and nice answer Jan 28, 2016 at 10:48
2

"The global contextual keyword, when it comes before the :: operator, refers to the global namespace, which is the default namespace for any C# program and is otherwise unnamed."

Source: https://msdn.microsoft.com/en-us/library/cc713620.aspx

1
  • Shami Qureshi, if you're going to copy an answer word-for-word, you need to at least mention the source. Although your answer really doesn't answer the question, I've edited it to include the URL which you copied it from.
    – johnnyRose
    Jan 8, 2016 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.