Reputation: 479
What does the global:: stand for in C#? for example what is the difference between
private global::System.Int32 myInt;
and
private int myInt;
Thanks
Upvotes: 10
Views: 4454
Reputation: 499152
It is the global namespace alias.
If you declare a type called System.Int32
in your codebase, you can distinguish the built in .NET one using this alias.
// your code
namespace System
{
public class Int32
{
}
}
// You could reference the BCL System.Int32 like this:
global::System.Int32 bclInt;
System.Int32 myInt;
See How to: Use the Global Namespace Alias (C# Programming Guide) on MSDN.
Upvotes: 6
Reputation: 28326
It is used to refer to the global namespace. This is useful if you're writing code in a namespace that already exists elsewhere.
See this for more info: http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx
Upvotes: 2
Reputation: 1502406
It's the "global" namespace - it forces the compiler to look for a name without taking other using directives into consideration. For example, suppose you had:
public class Bar{}
namespace Foo
{
public class Bar {}
public class Test
{
static void Main()
{
Bar bar1 = null; // Refers to Foo.Bar
global::Bar bar2 = null; // Refers to the "top level" Bar
}
}
}
Basically it's a way of avoiding naming collisions - you're most likely to see it in tool-generated code, where the tool doesn't necessarily know all the other types in the system. It's rarer to need it in manually-written code.
See "How to: Use the global namespace alias" on MSDN for more details, along with the ::
namespace qualifier.
Upvotes: 21