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

Community
  • 1
  • 1
Paul Fryer
  • 9,268
  • 14
  • 61
  • 93
  • 7
    @John, Just curious - trying to learn why it's used by the code-gen process. – Paul Fryer Aug 24 '10 at 00:43
  • 4
    Paul clearly wants to be a answerer at SO some day. Go Paul! – Hans Passant Aug 24 '10 at 00:51
  • 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 '10 at 00: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 '10 at 00: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. – Everything Matters Aug 18 '11 at 14:01
  • It's a namespace, not a keyword. – Mitch Wheat Aug 24 '10 at 00:41
  • 1
    So why would I use it? Resharper is telling me it's not needed, but Microsoft's code generation uses it. I'm trying to understand where I would ever use it. Thanks – Paul Fryer Aug 24 '10 at 00:42
  • 2
    @Paul: do not edit generated code. Not even in ReSharper. Generated code is a lot like sausage - you don't want to know what's in there. – John Saunders Aug 24 '10 at 00:44
  • 2
    @John, I'm not editing generated code, just trying to understand what it's doing. I have some code generation processes I'm working on, so I'd like to understand what others are doing any why. – Paul Fryer Aug 24 '10 at 00:48
  • Would the downvoter please leave a comment. Thanks. – Mitch Wheat Aug 24 '10 at 10:47
  • 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 01 '12 at 11:12

3 Answers3

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)

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
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.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
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

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
  • 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 08 '16 at 21:36