Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use the global keyword in C#?

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

like image 225
Paul Fryer Avatar asked Aug 24 '10 00:08

Paul Fryer


1 Answers

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)

  • http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx
like image 108
JaredPar Avatar answered Sep 20 '22 13:09

JaredPar