Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 reason for putting the using directives inside instead of outside the namespace?

like image 266
benPearce Avatar asked Sep 24 '08 03:09

benPearce


People also ask

Should using directive be inside or outside the namespace?

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.

What is using directive in namespace?

A using directive provides access to all namespace qualifiers and the scope operator. This is accomplished by applying the using keyword to a namespace identifier.

Why do we use the using directive in C# program?

The using directive allows you to abbreviate a type name by omitting the namespace portion of the name—such that just the type name can be specified for any type within the stated namespace.

Can we create alias of a namespace?

Namespace Alias Qualifier(::) makes the use of alias name in place of longer namespace and it provides a way to avoid ambiguous definitions of the classes. It is always positioned between two identifiers. The qualifier looks like two colons(::) with an alias name and the class name. It can be global.


1 Answers

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.

like image 178
Charlie Avatar answered Sep 23 '22 21:09

Charlie