It's not really common but there are some situations when it's handy.
Imagine you have this a class System
inside current namespace:
namespace Test {
static class System {
public static Do() { }
}
class Foo {
void foo() {
System.Do(); // What's this?
}
}
}
Do you want to make it more complicate? Add an inner class in System
and call it, for example, Action
. Of course these are edge cases and probably you won't ever need to use global::
but language itself has to handle this situation. See MSDN for more examples.
Global namespace aliases are also useful in another situation: when you reference two DLLs and they have the same namespaces and classes (for example because they simply are the two versions of same stuff). In that case how can you reference them? Same problem described here for C++. You can change namespace to include version number but it's a pain each time you change it (and VS automatic refactoring AFAIK doesn't work with namespaces) or you can reference them using two aliases. They'll be accessed, for example, like this:
Version1::CompanyName.MyNamespace.MyClass
Version2::CompanyName.MyNamespace.MyClass
Also note that it's a good practice (to include global::
namespace) when you're generating code (for example all designers generated code) because you don't know in which scenario that code will be compiled (then collisions may occur).
Reasoning about ::
and .
well...it's not a question for SO (unless you're so lucky Eric is having some fun here on SO) but I may guess it's because they're different things. If same operator .
is used then how can parser understand with global.System
you want to use global namespace alias and not a class or namespace named global
? They had to make global
a reserved keyword and it won't solve the other problem of conflicting hierarchies...