10

One thing I have noticed a lot of back and forth on is where using statements should be placed in a C# code file- whether its in the outermost scope or inside a namespace. I understand that the location of the using statement affects the scope of the references within that file, but what I don't understand is why, in most cases, someone would ever want their using statements inside their namespace.

In almost all cases only one namespace declaration ever exists in a single file so scoping the using statements seems/(is?) useless. If one were placing multiple types and multiple namespaces in the same file then scoped using statements make perfect sense, yet I still see plenty of cases of this being done even in files with one namespace. Why?

using System;

namespace MyNamespace
{
    using System.Text;

    public class MyClass {
        // ...
    }
}

An example of this being done throughout a project seemingly unnecessarily is the ASP.NET MVC source.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • 2
    This has really been beaten to death. The "why" of it is covered in this post: http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace – womp Aug 27 '09 at 18:22
  • Your SO-Search-Fu is greater than my own... I wasn't able to find that post when I searched for similar. :) – Nathan Taylor Aug 27 '09 at 18:23
  • Just to clarify, this question is actually about the [using Directive](http://msdn.microsoft.com/en-us/library/sf0df423(v=vs.110).aspx) and not the [using Statement](http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.110).aspx). – mwardm Dec 09 '13 at 12:39

3 Answers3

13

Putting "using" at the top of the files is the default way of Visual Studio. However, the recommended approach is putting the "using" statements inside of the namespace. Even MS's stylecop catches this and says the default way of VS is wrong.

Both techniques work fine.

StyleCop Rule says: 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.

Here's some links for further review:

Community
  • 1
  • 1
Jim W
  • 4,890
  • 2
  • 20
  • 26
  • 6
    Sorry, but this is plain wrong. For starters, `using` directives don't reference _assemblies_ - they import _namespaces_! For example, `namespace System` is present in assemblies `mscorlib.dll`, `System.dll` and `System.Core.dll`. Futhermore, C# `using` directives have absolutely no effect on output MSIL, because MSIL has all type names in full, always, and there is no concept of "namespace" at all on MSIL level. The effects observed in Scott's post most likely have to do with how _VS debugger_ handles loading of assemblies when it has the source code. – Pavel Minaev Aug 27 '09 at 18:25
  • 4
    Ahh, actually, now that I read Scott's blog post carefully, he is actually _debunking_ the claim. Specifically, he says: "If I do the same thing with the usings INSIDE the namespace I get identical results ... I'm 99.99% sure at this point that using directives can't change your assembly loading behavior and I think I was right to be suspicious. " – Pavel Minaev Aug 27 '09 at 18:27
  • 1
    Oops, bad quote.. Updated it. – Jim W Aug 27 '09 at 18:32
  • @Jim W you've misquoted Scott. He was quoting someone else before summarily proving them wrong. – grenade Aug 27 '09 at 18:32
  • but *why* is this the recommended approach? – Lucas Aug 27 '09 at 18:43
  • 1
    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. http://www.thewayithink.co.uk/stylecop/sa1200.htm – Jim W Aug 27 '09 at 18:55
3

I'd never even seen/heard of this practice until I started using StyleCop and would get flagged by rule SA1200, which I now just disable. It's odd that the .cs files that Visual Studio creates as part of a new project violate this rule by placing the using directives at the very beginning of the file, outside of the namespace.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
0

edited, with my head hanging in shame

Ahh! The using statement you're refering to is used to import a namespace, not to wrap an IDisposable object!

Very different, ambiguous terms... you had me confused :-)

Personally I like them outside the namespace at the top of the file; but it's probably due to me switching between C# and VB.NET.

I like to organize my projects into 1-file-per-class, no inner (nested) classes, and only one class per namespace (per file) . In this situation the location of the using statement is irrelevant whether inside or outside the namespace.


The iDesign C# coding standard is a solid standard to follow (or to derive your own from). It recommends keeping the using statements outside the namespace as item #14. But it's all down to your company / project's convention

STW
  • 44,917
  • 17
  • 105
  • 161