3

Coming from C++ background myself, it takes some time to get used to C#. In C#, one uses the dot operator (.) more often than the ::-operator (the namespace qualifier operator in C#), to access namespace and class scopes.

In C++, one uses the dot operator (.) to access members of an instance of a class. Scope resolution operator (::) on the other hand, is used to access members of a class without an instance of that class. To me, this makes sense and is both logical and consistent.

While I can accept different approach used in C#, there seems to be at least one instance, in which I see an inconsistence. At least, that is how it appears to me. It has to do with the global keyword:

global::System.Console.WriteLine("Hello World");

Could somebody explain to me why namespace alias qualifier is required to use with global keyword instead of the dot operator?

Henri Korpela
  • 123
  • 1
  • 5
  • Because `global` is a *namespace alias* to the (actually anonymous) global namespace, not a namespace *per se*? – Frédéric Hamidi Nov 04 '14 at 08:52
  • Always treat resembles between C# and C++ as an accident, not a design goal. And the odds it does the same inversely proportional to the amount of trouble it causes in C++. Which is a *lot*, name resolution in C++ is ridiculously complicated. The Microsoft C++ compiler still has major bugs today. – Hans Passant Nov 04 '14 at 12:00

1 Answers1

1

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...

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • 2
    Of course anyone creating their own class named 'System' needs to have their head examined. If you find that you need to use 'global::', then you've probably done something wrong. – Dave Doknjas Nov 04 '14 at 15:06
  • @DaveDoknjas absolutely! It's little bit _worse_ because same consideration applies to properties, inner classes and fields but yes...pretty uncommon. – Adriano Repetti Nov 04 '14 at 15:14