Should 'using' be inside the namespace or outside? [duplicate]

孤者浪人 提交于 2020-01-24 03:21:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!