Sunday, May 27, 2012

Should Usings be inside or outside the namespace


I have been running StyleCop over some C# code and it keeps reporting that my using statements should be inside the namespace.



Is there a technical reason for putting the using statements inside instead of outside the namespace?


Source: Tips4all

6 comments:

  1. 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 statements 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 statements.

    ReplyDelete
  2. Putting it inside the namespaces makes the declarations local to that namespace for the file (in case you have multiple namespaces in the file) but if you only have one namespace per file then it doesn't make a difference whether they go outside or inside the namespace.

    using ThisNamespace.IsImported.InAllNamespaces.Here;

    namespace Namespace1
    { using ThisNamespace.IsImported.InNamespace1.AndNamespace2;

    namespace Namespace2
    { using ThisNamespace.IsImported.InJustNamespace2;
    }
    }

    namespace Namespace3
    { using ThisNamespace.IsImported.InJustNamespace3;
    }

    ReplyDelete
  3. Here is some information.. My style is to put them outside the namespaces.

    http://www.hanselman.com/blog/BackToBasicsDoNamespaceUsingDirectivesAffectAssemblyLoading.aspx

    ReplyDelete
  4. According the to StyleCop Documentation:

    SA1200: UsingDirectivesMustBePlacedWithinNamespace

    Cause
    A C# using directive is placed outside of a namespace element.

    Rule Description
    A violation of this rule occurs when a using directive or a using-alias directive is placed outside of a namespace element, unless the file does not contain any namespace elements.

    For example, the following code would result in two violations of this rule.

    using System;
    using Guid = System.Guid;

    namespace Microsoft.Sample
    {
    public class Program
    {
    }
    }


    The following code, however, would not result in any violations of this rule:

    namespace Microsoft.Sample
    {
    using System;
    using Guid = System.Guid;

    public class Program
    {
    }
    }


    This code will compile cleanly, without any compiler errors. However, it is unclear which version of the Guid type is being allocated. If the using directive is moved inside of the namespace, as shown below, a compiler error will occur:

    namespace Microsoft.Sample
    {
    using Guid = System.Guid;
    public class Guid
    {
    public Guid(string s)
    {
    }
    }

    public class Program
    {
    public static void Main(string[] args)
    {
    Guid g = new Guid("hello");
    }
    }
    }


    The code fails on the following compiler error, found on the line containing Guid g = new Guid("hello");

    CS0576: Namespace 'Microsoft.Sample' contains a definition conflicting with alias 'Guid'

    The code creates an alias to the System.Guid type called Guid, and also creates its own type called Guid with a matching constructor interface. Later, the code creates an instance of the type Guid. To create this instance, the compiler must choose between the two different definitions of Guid. When the using-alias directive is placed outside of the namespace element, the compiler will choose the local definition of Guid defined within the local namespace, and completely ignore the using-alias directive defined outside of the namespace. This, unfortunately, is not obvious when reading the code.

    When the using-alias directive is positioned within the namespace, however, the compiler has to choose between two different, conflicting Guid types both defined within the same namespace. Both of these types provide a matching constructor. The compiler is unable to make a decision, so it flags the compiler error.

    Placing the using-alias directive outside of the namespace is a bad practice because it can lead to confusion in situations such as this, where it is not obvious which version of the type is actually being used. This can potentially lead to a bug which might be difficult to diagnose.

    Placing using-alias directives within the namespace element eliminates this as a source of bugs.


    Multiple Namespaces


    Placing multiple namespace elements within a single file is generally a bad idea, but if and when this is done, it is a good idea to place all using directives within each of the namespace elements, rather than globally at the top of the file. This will scope the namespaces tightly, and will also help to avoid the kind of behavior described above.

    It is important to note that when code has been written with using directives placed outside of the namespace, care should be taken when moving these directives within the namespace, to ensure that this is not changing the semantics of the code. As explained above, placing using-alias directives within the namespace element allows the compiler to choose between conflicting types in ways that will not happen when the directives are placed outside of the namespace.

    How to Fix Violations
    To fix a violation of this rule, move all using directives and using-alias directives within the namespace element.

    ReplyDelete
  5. Sometimes it makes difference where you put usings: http://stackoverflow.com/questions/292535/linq-to-sql-designer-bug

    ReplyDelete
  6. The accepted answer is good, but to me it seems like a good reason to put the using clauses outside the namespace. If I'm in namespace Outer.Inner I would expect it to use the Math class from Outer.Inner and not System.Math.

    ReplyDelete