【问题标题】:Should 'using' be inside the namespace or outside? [duplicate]“使用”应该在命名空间内部还是外部? [复制]
【发布时间】:2011-11-09 11:17:46
【问题描述】:

可能重复:
Should Usings be inside or outside the namespace

是否有任何技术原因偏爱这个

namespace Foo
{
     using System;
     using System.IO;

而不是默认的

using System;
using System.IO;

namespace Foo
{

【问题讨论】:

标签: c# .net coding-style


【解决方案1】:

埃里克·利珀特explains this.

一般来说,它们是相同的。
但是,命名空间中的using 语句可以看到命名空间之外包含的命名空间和别名。

【讨论】:

    【解决方案2】:

    几乎* 两者之间的唯一区别是如果您在同一个文件中使用了多个命名空间(或者如果您多次使用相同的命名空间)。我不知道你为什么要这样做,但你当然可以:

    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
        }
    }
    

    * 参见 SLaks 的回答。

    【讨论】:

    【解决方案3】:

    没有技术原因,只是偏好。当然,第二段代码看起来更干净。

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 2018-06-30
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 2021-01-05
      • 2014-10-28
      • 1970-01-01
      相关资源
      最近更新 更多