views:

585

answers:

4

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.

A: 

I believe the best practice is to have using statements outside of namespace scope.

I've never done otherwise, and I would be hardpressed to consider doing it any other way.

This is what I would do:

using System;

namespace MyNamespace
{
    public class MyClass {
            // ...
    }
}

This is what I wouldn't do:

namespace MyNamespace
{
    using System.Text;

    public class MyClass {
            // ...
    }
}
Joseph
A: 

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
That's not what he's referring to.
womp
"only one class per namespace" - Really?
grenade
That made me chuckle when I read it too. :D
Nathan Taylor
@grenade: yeah, that's a typo... the intended meaning would have been redundant, but would have been "one class per namespace per file". Whoops!
STW
+8  A: 

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:

Jim W
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
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
Oops, bad quote.. Updated it.
Jim W
@Jim W you've misquoted Scott. He was quoting someone else before summarily proving them wrong.
grenade
Yep, I edited the quote as soon as I realized it.
Jim W
but *why* is this the recommended approach?
Lucas
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
+3  A: 

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.

BACON