views:

378

answers:

5

From what I understand, the global:: qualifier allows you to access a namespace that has been hidden by another with the same name. The MSDN page uses System as an example. If you create your own namespace System, you can reach the original with global::System. The first thing that came to mind is, why would anyone call their namespace System?? The page goes on to say this is obviously not recommended, but namespace duplication is very possible in large projects. If/when this does occur, is it a sign that things are heading in the wrong direction, or are there valid reasons to have conflicting namespaces?

+8  A: 

In general, global:: is used to indicate "I want to start at the top of the namespace structure". If I have a namespace called MyProduct.System, then anything that resides in the MyProduct namespace will not be able to access the Microsoft System namespace. Is it a code smell? Maybe sometimes, but not particularly smelly.

Adam Robinson
+6  A: 

Any machine generated code should try and use global:: in order to minimize potential for namespace conflicts it may not be aware about. Moreover, any of your code which may run into conflicts can use it to be more specific.

sixlettervariables
+7  A: 

One legitimate reason for having conflicting namespaces might be the use of in-house libraries that were written for earlier versions of .Net that didn't contain functionality that was added in later versions. In the .Net 1.1 days, for example, I wrote a Registry class that wrapped API registry calls. By pure chance, the method names that I chose were exactly the same as those in the later .Net Registry class, and they did exactly the same things, so it was easy to unplug my homegrown code. For more complicated stuff, being able to use an older, poorly-named chunk of code with the global:: qualifier could be useful.

Intentionally naming a new piece of code using an existing .Net namespace would definitely be a code smell, however.

MusiGenesis
+1 old Net 1.1 schtuff
sixlettervariables
A: 

I think it just happens from time to time that one of your namespaces has the name of another. For example, I have a namespace .Persistence.NHibernate, where NHibernate could also be the root namespace of the NHibernate assembly.

I don't see any code smell here, it's just naming isses ;)

Oliver Hanappi
+2  A: 

Microsoft has some good namespace guidelines in the excellent book Framework Design Guidelines 2nd Ed.. In general they recommend against introducing comflicts (for instance, by naming your type Stream).

I don't believe I've ever used the global:: qualifier. I would generally consider it a code smell (although there are exceptions, as MusiGenesis and sixlettervariables point out).

TrueWill