C# - using 语句的位置

发布于 2024-08-03 07:48:17 字数 589 浏览 2 评论 0原文

我反复注意到的一件事是 using 语句应该放置在 C# 代码文件中的何处 - 无论是在最外层作用域还是在命名空间内。我知道 using 语句的位置会影响该文件内引用的范围,但我不明白的是为什么在大多数情况下,有人会希望他们的 using 语句位于其命名空间内。

几乎所有情况中,单个文件中只存在一个名称空间声明,因此确定 using 语句的范围似乎/(是?)无用。如果将多种类型和多个命名空间放在同一个文件中,那么范围化的 using 语句就非常有意义,但我仍然看到很多这样的情况,即使在具有一个命名空间的文件中也是如此。为什么?

using System;

namespace MyNamespace
{
    using System.Text;

    public class MyClass {
        // ...
    }
}

在整个项目中看似不必要地完成此操作的一个示例是 ASP.NET MVC 源

One thing I have noticed a lot of back and forth on is where using statements should be placed in a C# code file- whether its in the outermost scope or inside a namespace. I understand that the location of the using statement affects the scope of the references within that file, but what I don't understand is why, in most cases, someone would ever want their using statements inside their namespace.

In almost all cases only one namespace declaration ever exists in a single file so scoping the using statements seems/(is?) useless. If one were placing multiple types and multiple namespaces in the same file then scoped using statements make perfect sense, yet I still see plenty of cases of this being done even in files with one namespace. Why?

using System;

namespace MyNamespace
{
    using System.Text;

    public class MyClass {
        // ...
    }
}

An example of this being done throughout a project seemingly unnecessarily is the ASP.NET MVC source.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃→可爱长大的 2024-08-10 07:48:17

将“using”放在文件顶部是 Visual Studio 的默认方式。但是,推荐的方法是将“using”语句放在命名空间内。甚至 MS 的 stylecop 也发现了这一点,并说 VS 的默认方式是错误的。

两种技术都可以正常工作。

StyleCop 规则说:
放置多个命名空间元素
单个文件内通常是
坏主意,但如果这是
完成后,最好将所有内容都放置在
在每个中使用指令
命名空间元素,而不是
全局位于文件顶部。这
将严格限制命名空间,并且
也将有助于避免这种情况
上述行为。

需要注意的是,当代码
已经用 using 指令编写
放置在命名空间之外,小心
移动这些时应采取
命名空间内的指令,以
确保这不会改变
代码的语义。正如所解释的
上面,放置了 using-alias 指令
命名空间元素内允许
编译器之间进行选择
冲突类型的方式
当指令执行时不会发生
放置在命名空间之外。

以下是一些供进一步审查的链接:

Putting "using" at the top of the files is the default way of Visual Studio. However, the recommended approach is putting the "using" statements inside of the namespace. Even MS's stylecop catches this and says the default way of VS is wrong.

Both techniques work fine.

StyleCop Rule says:
Placing multiple namespace elements
within a single file is generally a
bad idea, but if and when this is
done, it is a good idea to place all
using directives within each of the
namespace elements, rather than
globally at the top of the file. This
will scope the namespaces tightly, and
will also help to avoid the kind of
behavior described above.

It is important to note that when code
has been written with using directives
placed outside of the namespace, care
should be taken when moving these
directives within the namespace, to
ensure that this is not changing the
semantics of the code. As explained
above, placing using-alias directives
within the namespace element allows
the compiler to choose between
conflicting types in ways that will
not happen when the directives are
placed outside of the namespace.

Here's some links for further review:

不再见 2024-08-10 07:48:17

在我开始使用 StyleCop 之前,我什至从未见过/听说过这种做法,并且会被 rule SA1200,我现在只是禁用它。奇怪的是,Visual Studio 作为新项目的一部分创建的 .cs 文件违反了此规则,将 using 指令放置在文件的最开头、命名空间之外。

I'd never even seen/heard of this practice until I started using StyleCop and would get flagged by rule SA1200, which I now just disable. It's odd that the .cs files that Visual Studio creates as part of a new project violate this rule by placing the using directives at the very beginning of the file, outside of the namespace.

无声无音无过去 2024-08-10 07:48:17

编辑完毕,我羞愧地低着头

啊!您引用的 using 语句用于导入命名空间,而不是包装 IDisposable 对象!

非常不同、模棱两可的术语...你让我感到困惑:-)

就我个人而言,我喜欢它们位于文件顶部的命名空间之外;但这可能是由于我在 C# 和 VB.NET 之间切换所致。

我喜欢将我的项目组织为每个类 1 个文件,没有内部(嵌套)类,并且每个命名空间只有一个类(每个文件)。在这种情况下,using 语句的位置无论在命名空间内部还是外部都是无关紧要的。


iDesign C# 编码标准 是一个值得遵循的可靠标准(或从中派生出自己的标准)。它建议将 using 语句保留在命名空间之外,如第 14 项所示。但这完全取决于您的公司/项目的惯例

edited, with my head hanging in shame

Ahh! The using statement you're refering to is used to import a namespace, not to wrap an IDisposable object!

Very different, ambiguous terms... you had me confused :-)

Personally I like them outside the namespace at the top of the file; but it's probably due to me switching between C# and VB.NET.

I like to organize my projects into 1-file-per-class, no inner (nested) classes, and only one class per namespace (per file) . In this situation the location of the using statement is irrelevant whether inside or outside the namespace.


The iDesign C# coding standard is a solid standard to follow (or to derive your own from). It recommends keeping the using statements outside the namespace as item #14. But it's all down to your company / project's convention

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文