Should 'using' be inside the namespace or

2019-06-17 03:30发布

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
{

3条回答
疯言疯语
2楼-- · 2019-06-17 04:05

No technical reason, just a preference. of course the second chunk of code looks cleaner, though.

查看更多
ら.Afraid
3楼-- · 2019-06-17 04:16

Eric Lippert explains this.

In general, they're identical.
However, using statements in the namespace can see namespaces and aliases included outside the namespace.

查看更多
狗以群分
4楼-- · 2019-06-17 04:27

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.

查看更多
登录 后发表回答