[c#] Should 'using' directives be inside or outside the namespace?

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:

  • It provides a visual distinction between local modules and 'other' modules, meaning everything else.
  • It scopes the local directives to be applied preferentially over global directives.

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.

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to namespaces

Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model How do I get an object's unqualified (short) class name? socket.error:[errno 99] cannot assign requested address and namespace in python What is the use of "using namespace std"? Visibility of global variables in imported modules Using :: in C++ 'namespace' but is used like a 'type' type object 'datetime.datetime' has no attribute 'datetime' Why am I getting error CS0246: The type or namespace name could not be found? string in namespace std does not name a type

Examples related to stylecop

Should 'using' directives be inside or outside the namespace?

Examples related to code-organization

Including one C source file in another? Should 'using' directives be inside or outside the namespace?