I have been running StyleCop over some C# code, and it keeps reporting that my using
directives should be inside the namespace.
Is there a technical rea
When citing Microsoft's internal guidelines, keep in mind that they're written by somebody who probably has less than ten years experience coding. In other words, they're likely based on nothing more solid than personal preference. Especially in something like C# that's so new.
As a rule, external using
directives (System and Microsoft namespaces for example) should be placed outside the namespace
directive. They are defaults that should be applied in all cases unless otherwise specified. This should include any of your own organization's internal libraries that are not part of the current project, or using
directives that reference other primary namespaces in the same project. Any using
directives that reference other modules in the current project and namespace should be placed inside the namespace
directive. This serves two specific functions:
The latter reason is significant. It means that it's harder to introduce an ambiguous reference issue that can be introduced by a change no more significant than refactoring code. That is to say, you move a method from one file to another and suddenly a bug shows up that wasn't there before. Colloquially, a 'heisenbug' - historically fiendishly difficult to track down.
As an even more general rule, a good one to follow is this. If you see something intrinsic to a language that seems to be a useless option, assume that it's NOT. In fact, the harder it is to see why the option exists, the more important you should assume it is. Do the research about the specific differences between the two options and then think long and hard about the implications. You'll usually find an amazingly insightful and clever solution to an obscure problem that the language designer put in there specifically to make your life easier. Be appropriately grateful and take advantage of it.