C# - using 语句的位置
我反复注意到的一件事是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将“using”放在文件顶部是 Visual Studio 的默认方式。但是,推荐的方法是将“using”语句放在命名空间内。甚至 MS 的 stylecop 也发现了这一点,并说 VS 的默认方式是错误的。
两种技术都可以正常工作。
以下是一些供进一步审查的链接:
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.
Here's some links for further review:
在我开始使用 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.
编辑完毕,我羞愧地低着头
啊!您引用的
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 anIDisposable
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