问题
Possible Duplicate:
Should Usings be inside or outside the namespace
Are there any technical reasons for preferring this
namespace Foo
{
using System;
using System.IO;
instead of the default
using System;
using System.IO;
namespace Foo
{
回答1:
Eric Lippert explains this.
In general, they're identical.
However, using
statements in the namespace can see namespaces and aliases included outside the namespace.
回答2:
Almost* the only difference between the two would be if you used more than one namespace in the same file (or if you used the same namespace more than once). I'm not sure why would you do that, bu you certainly can:
using System;
namespace FooNamespace
{
using System.IO;
class Foo
{
// you can use types from System and System.IO directly here
}
}
namespace BarNamespace
{
class Bar
{
// you can't use types from System.IO directly here
// but you can use types from System
}
}
* See SLaks' answer.
回答3:
No technical reason, just a preference. of course the second chunk of code looks cleaner, though.
来源:https://stackoverflow.com/questions/7288835/should-using-be-inside-the-namespace-or-outside