views:

114

answers:

5

Possible Duplicate:
Should Usings be inside or outside the namespace

I am looking at a code base where the author (one I respect) consistently places using statements inside of the namespace, as opposed to above it. Is there some advantage (more efficient GC?) to doing so or is this just a code style preference?

Cheers,
Berryl

A: 

If you have multiple namespaces in the same file then you will be scoping usings only to the containing namespace instead of to all namespace in the entire file.

Also see (just found this good explanation) http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace

John K
But if you're adhering to the 1 class per file guideline, this is a non-issue.
Anthony Pegram
@Anthony: Exactly.
John K
+2  A: 

Never put them inside without using "global::" or your code will become brittle.

namspace bar {
   using foo //this may mean "using global::bar.foo OR using global::foo"

}

Reference

http://blogs.msdn.com/b/ericlippert/archive/2007/06/25/inside-or-outside.aspx?wa=wsignin1.0

Jonathan Allen
Well, in *practice* it's hardly ever a problem. People generally don't make namespaces with weird collisions. I've never been bitten by this in real life.
Eric Lippert
I suspect the developers you work with are a wee bit more sophisticated than the ones the rest of us deal with. And even then we ended up with both System.Data and System.Windows.Data, which really messes with VB import rules.
Jonathan Allen
A: 

It's a preference thing but there is a semantic difference when you use the statement on the inside versus on the outside in some scenarios.

using Bar;

namespace Foo
{
    using Bar;

    namespace Bar
    {
        class C
        {
        }

    }

    namespace Baz
    {
        class D
        {
            C c = new C();
        }
    }
}

namespace Bar
{
    class E
    {
    }
}

In this, the outer using statement refers to the namespace Bar that is after namespace Foo. The inner using statement refers to Bar that is inside Foo. If there were no Bar inside Foo, then the inner would also refer to the outer Bar.

Edit And as Jonathan points out, the inner using can be changed to `using global::Bar;" to refer to the out Bar namespace, which would happen to break this particular code because of D trying to use C.

Anthony Pegram
A: 

It's MS recommend practice. Programs such as stylecop recommend it.

Check out http://stackoverflow.com/questions/1071797/is-sa1200-all-using-directives-must-be-placed-inside-the-namespace-stylecop-pur for a more in-depth discussion

Brad Bruce
StyleCop isn't Microsoft, it is just a tool written by a couple people at Microsoft.
Jonathan Allen
+1  A: 

Scott Hanselman did a post about this back in July 2008. I don't know if this changed with the .NET 4 framework, but it basically came down to being a preference issue unless you're naming your classes the same as existing classes as well as multiple namespaces in a single file.

Agent_9191