Should 'Using' Directives Be Inside or Outside the Namespace

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

There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs:

// File1.cs
using System;
namespace Outer.Inner
{
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}

Now imagine that someone adds another file (File2.cs) to the project that looks like this:

// File2.cs
namespace Outer
{
class Math
{
}
}

The compiler searches Outer before looking at those using directives outside the namespace, so it finds Outer.Math instead of System.Math. Unfortunately (or perhaps fortunately?), Outer.Math has no PI member, so File1 is now broken.

This changes if you put the using inside your namespace declaration, as follows:

// File1b.cs
namespace Outer.Inner
{
using System;
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}

Now the compiler searches System before searching Outer, finds System.Math, and all is well.

Some would argue that Math might be a bad name for a user-defined class, since there's already one in System; the point here is just that there is a difference, and it affects the maintainability of your code.

It's also interesting to note what happens if Foo is in namespace Outer, rather than Outer.Inner. In that case, adding Outer.Math in File2 breaks File1 regardless of where the using goes. This implies that the compiler searches the innermost enclosing namespace before it looks at any using directive.

Place usings inside or outside of namespace

There is a difference, small but there is.

It's all about the sequence of name resolution made by compiler.
The good answer on subject you can find here:

Should Usings be inside or outside the namespace

In practise in first case compiler, in case could not find a type information immediately,
would search among namespaces declared inside using. In second case, instead, would search first among the actual namespace and after only go to search inside declared outside.

using' directives be inside the namespace

Have you tried You need to setup your preferred placement. You can find this option in the following path:

  • Options
  • Texteditor
  • C#
  • Code Style
  • change Preferred 'using' directive placement to Inside namespace

Preferences

Should I put the using namespace inside or outside of a namespace in C++

In header file you should never have a using namespace N; directive in the global scope.

It will force lots of identifiers from N on all client code.

But it can be okay to have it inside a namespace X. Just keep in mind that client code that does using namespace X; will then also get all identifiers from N. A more conservative approach is to have a bunch of using declarations in namespace X, e.g. using N::foo;.


An alternative, when the reason for using namespace N; is that N is a very long name, like Not_short_enough_for_practical_use, is to use a namespace alias – preferably inside the smallest scope where it's needed:

namespace N = Not_short_enough_for_practical_use;

Your teacher's “correction” of moving a using namespace out of a namespace, before it, is of negative value.

You should always strive (within practical limits) to minimize the scope of anything.

Stop Visual Studio from putting using directives outside namespace

Generally I don't believe there's any harm in including using statementents at the top of your class. I actually find it easier to include them there, so it's up to you whether you want to respect that rule.

If you do however, all of the file templates are available and can be edited. See the answer How do I edit the Visual Studio templates for new C# class/interface? to detail where they live on each Visual Studio version.

Once you're there you can change the layout, so for example a basic class looks like:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}

You could change this to the following or similar:

namespace $rootnamespace$
{
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$

class $safeitemrootname$
{
}
}

There may be quite a few files to change though!

When do we need to put using directives inside a namespace scope?

See Should 'using' statements be inside or outside the namespace?

How to move the using directive inside the namespace on formatting?

You can actually change this in the settings directly:

Text Editor -> C# -> Code Style -> General -> 'using preferences:'

There you can change Preferred 'using' directive placement from Outside namepace to Inside namespace.

Sample Image

In addition you can also click on the light bulb next to a misplaced using directive and choose "Move misplaced using directives" and there, in the opening submenu "Fix all occurrences in: Document | Project | Solution" to select "Solution".

Sample Image

Namespaces and Using Directives

No, it's only good for the namespace section inside the file. Not for all files inside the namespace.

If you put the using statement outside the namespace, then it applies to the entire file regardless of namespace.

It will also search the Usings inside the namespace first, before going to the outer scope.



Related Topics



Leave a reply



Submit