【问题标题】:When do we need to put using directives inside a namespace scope? [duplicate]我们什么时候需要将 using 指令放在命名空间范围内? [复制]
【发布时间】:2011-06-28 20:58:58
【问题描述】:

我不知道为什么 Asp.net MVC 开发人员将using 指令放在System.Web.Mvc 命名空间中,如下所示。

namespace System.Web.Mvc
{
    using System;
    using System.Collections.ObjectModel;

    [Serializable]
    public class ModelErrorCollection : Collection<ModelError>
    {

        public void Add(Exception exception)
        {
            Add(new ModelError(exception));
        }

        public void Add(string errorMessage)
        {
            Add(new ModelError(errorMessage));
        }
    }
}

我们什么时候需要将using 指令放入namespace 范围内?

【问题讨论】:

  • 我什至不知道是否有必要。我能想到的唯一原因可能是如果您有一个源文件,其中包含多个命名空间。但是人们真的经常这样做吗?我希望不大。

标签: c# .net namespaces code-organization using-directives


【解决方案1】:
【解决方案2】:

这是你的选择。 StyleCop 工具警告说这是最佳实践,但是 Visual Studio 生成的文件总是在命名空间之外使用 usings。

Should all the using directives for namespaces be inside the namespace?

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 2022-07-06
    • 2014-07-13
    • 1970-01-01
    • 2011-09-03
    • 2011-04-17
    • 2012-01-04
    • 2014-12-25
    相关资源
    最近更新 更多