应该“使用” C# 中的指令位于命名空间内部还是外部?
我一直在一些 C# 代码上运行 StyleCop ,并且它不断报告我的 using< /code> 指令应该位于命名空间内。
将 using
指令放在命名空间内部而不是外部是否有技术原因?
I have been running StyleCop over some C# code, and it keeps reporting that my using
directives should be inside the namespace.
Is there a technical reason for putting the using
directives inside instead of outside the namespace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
两者之间实际上存在(微妙的)差异。假设您在 File1.cs 中有以下代码:
现在假设有人将另一个文件 (File2.cs) 添加到项目中,如下所示:
编译器在查看这些
之前先搜索
指令位于命名空间之外,因此它会查找Outer
,然后使用Outer.Math
而不是System.Math
。不幸的是(或许幸运的是?),Outer.Math
没有PI
成员,因此 File1 现在已损坏。如果您将
using
放入命名空间声明中,情况会发生变化,如下所示:现在,编译器在搜索
Outer
之前搜索System
,找到System .数学
,一切都很好。有些人会认为,对于用户定义的类来说,
Math
可能是一个不好的名字,因为System
中已经有一个这样的名称;这里的要点是存在差异,它会影响代码的可维护性。值得注意的是,如果
Foo
位于命名空间Outer
中,而不是Outer.Inner
中,会发生什么情况。在这种情况下,无论using
位于何处,在 File2 中添加Outer.Math
都会破坏 File1。这意味着编译器在查看任何using
指令之前会先搜索最内层的封闭命名空间。There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs:
Now imagine that someone adds another file (File2.cs) to the project that looks like this:
The compiler searches
Outer
before looking at thoseusing
directives outside the namespace, so it findsOuter.Math
instead ofSystem.Math
. Unfortunately (or perhaps fortunately?),Outer.Math
has noPI
member, so File1 is now broken.This changes if you put the
using
inside your namespace declaration, as follows:Now the compiler searches
System
before searchingOuter
, findsSystem.Math
, and all is well.Some would argue that
Math
might be a bad name for a user-defined class, since there's already one inSystem
; the point here is just that there is a difference, and it affects the maintainability of your code.It's also interesting to note what happens if
Foo
is in namespaceOuter
, rather thanOuter.Inner
. In that case, addingOuter.Math
in File2 breaks File1 regardless of where theusing
goes. This implies that the compiler searches the innermost enclosing namespace before it looks at anyusing
directive.该线程已经有一些很好的答案,但我觉得我可以通过这个附加答案带来更多细节。
首先,请记住带有句点的命名空间声明,例如:
完全等同于:
如果您愿意,您可以将
using
指令放在所有这些级别上。 (当然,我们希望只在一个地方使用using
,但根据该语言,这是合法的。)解析隐含类型的规则可以这样宽松地表述:< strong>首先在最里面的“范围”中搜索匹配项,如果没有找到任何内容,则转到下一个范围并在那里搜索,依此类推,直到找到匹配项。如果在某一级别找到多个匹配项,并且其中一种类型来自当前程序集,则选择该类型并发出编译器警告。否则,放弃(编译时错误)。
现在,让我们通过两个主要约定的具体示例来明确说明这意味着什么。
(1) 在外部使用:
在上述情况下,要找出
Ambigously
是什么类型,请按照以下顺序进行搜索:C
内部的嵌套类型code> (包括继承的嵌套类型)MyCorp.TheProduct.SomeModule.Utilities
MyCorp.TheProduct.SomeModule
MyCorp.TheProduct
MyCorp
中的类型System
、System.Collections.Generic
、System.Linq
、MyCorp.TheProduct.OtherModule
、MyCorp.TheProduct.OtherModule.Integration
和ThirdParty
另一个约定:
(2) 内部使用:
现在,搜索类型
Ambigitude
按以下顺序进行:C
内的嵌套类型(包括继承的嵌套类型)MyCorp.TheProduct.SomeModule.Utilities
系统
、System.Collections.Generic
、System.Linq
、MyCorp.TheProduct
、MyCorp.TheProduct 命名空间中的.OtherModule
、MyCorp.TheProduct.OtherModule.Integration
和ThirdParty
MyCorp.TheProduct.SomeModule
MyCorp
中的类型(请注意,
MyCorp.TheProduct 是“3.”的一部分,因此不需要在“4.”和“5.”之间。)
结束语
无论将 using 放在名称空间声明内部还是外部,总有可能以后有人将具有相同名称的新类型添加到具有更高优先级的命名空间之一。
此外,如果嵌套命名空间与类型具有相同的名称,则可能会导致问题。
将使用从一个位置移动到另一个位置总是很危险的,因为搜索层次结构会发生变化,并且可能会找到另一种类型。因此,选择一种约定并坚持下去,这样您就不必移动使用。
默认情况下,Visual Studio 的模板将 usings 放置在命名空间之外(例如,如果您让 VS 在新文件中生成新类)。
在外部使用 using 的一个(微小)优点是,您可以将 using 指令用于全局属性,例如
[assemble: ComVisible(false)]
而不是 <代码>[程序集:System.Runtime.InteropServices.ComVisible(false)]。受其他线程启发的添加:假设在上面的示例中,命名空间
MyCorp.TheProduct.System
发生了存在,即使我们的文件中没有使用它。然后在外面使用,它不会改变任何东西。但是使用 usings inside 时,您必须使用global
别名,如下所示:更新有关文件范围的命名空间声明
自 C# 10.0(从2021),您可以避免缩进并使用(约定 1,外部使用):
或(约定 2,内部使用):
但与之前的注意事项相同。
This thread already has some great answers, but I feel I can bring a little more detail with this additional answer.
First, remember that a namespace declaration with periods, like:
is entirely equivalent to:
If you wanted to, you could put
using
directives on all of these levels. (Of course, we want to haveusing
s in only one place, but it would be legal according to the language.)The rule for resolving which type is implied, can be loosely stated like this: First search the inner-most "scope" for a match, if nothing is found there go out one level to the next scope and search there, and so on, until a match is found. If at some level more than one match is found, if one of the types are from the current assembly, pick that one and issue a compiler warning. Otherwise, give up (compile-time error).
Now, let's be explicit about what this means in a concrete example with the two major conventions.
(1) With usings outside:
In the above case, to find out what type
Ambiguous
is, the search goes in this order:C
(including inherited nested types)MyCorp.TheProduct.SomeModule.Utilities
MyCorp.TheProduct.SomeModule
MyCorp.TheProduct
MyCorp
System
,System.Collections.Generic
,System.Linq
,MyCorp.TheProduct.OtherModule
,MyCorp.TheProduct.OtherModule.Integration
, andThirdParty
The other convention:
(2) With usings inside:
Now, search for the type
Ambiguous
goes in this order:C
(including inherited nested types)MyCorp.TheProduct.SomeModule.Utilities
System
,System.Collections.Generic
,System.Linq
,MyCorp.TheProduct
,MyCorp.TheProduct.OtherModule
,MyCorp.TheProduct.OtherModule.Integration
, andThirdParty
MyCorp.TheProduct.SomeModule
MyCorp
(Note that
MyCorp.TheProduct
was a part of "3." and was therefore not needed between "4." and "5.".)Concluding remarks
No matter if you put the usings inside or outside the namespace declaration, there's always the possibility that someone later adds a new type with identical name to one of the namespaces which have higher priority.
Also, if a nested namespace has the same name as a type, it can cause problems.
It is always dangerous to move the usings from one location to another because the search hierarchy changes, and another type may be found. Therefore, choose one convention and stick to it, so that you won't have to ever move usings.
Visual Studio's templates, by default, put the usings outside of the namespace (for example if you make VS generate a new class in a new file).
One (tiny) advantage of having usings outside is that you can then utilize the using directives for a global attribute, for example
[assembly: ComVisible(false)]
instead of[assembly: System.Runtime.InteropServices.ComVisible(false)]
.Addition inspired by other thread: Suppose in the above example that the namespace
MyCorp.TheProduct.System
happened to exist, even though we have no use for it in our file. Then with usings outside, it would not change anything. But with usings inside, you would have to use theglobal
alias, like this:Update about file-scoped namespace declarations
Since C# 10.0 (from 2021), you can avoid indentation and use either (convention 1, usings outside):
or (convention 2, usings inside):
But the same considerations as before apply.
将其放入命名空间内会使声明成为该文件的该命名空间的本地声明(如果文件中有多个命名空间),但如果每个文件只有一个命名空间,那么无论它们是在外部还是外部,都没有太大区别在命名空间内。
Putting it inside the namespaces makes the declarations local to that namespace for the file (in case you have multiple namespaces in the file) but if you only have one namespace per file then it doesn't make much of a difference whether they go outside or inside the namespace.
根据 Hanselman - 使用指令和程序集加载... 和其他此类文章,技术上有没有区别。
我的偏好是将它们放在名称空间之外。
According to Hanselman - Using Directive and Assembly Loading... and other such articles there is technically no difference.
My preference is to put them outside of namespaces.
根据 StyleCop 文档:
SA1200:UsingDirectivesMustBePlacedWithinNamespace
原因
C# using 指令放置在命名空间元素之外。
规则说明
当 using 指令或 using-alias 指令放置在命名空间元素之外时,就会违反此规则,除非该文件不包含任何命名空间元素。
例如,以下代码将导致两次违反此规则。
但是,以下代码不会导致违反此规则:
此代码将干净地编译,没有任何编译器错误。但是,尚不清楚正在分配哪个版本的 Guid 类型。如果将 using 指令移动到命名空间内部,如下所示,将会发生编译器错误:
代码因以下编译器错误而失败,该错误出现在包含
Guid g = new Guid("hello");< 的行上/code>
CS0576:命名空间“Microsoft.Sample”包含与别名“Guid”冲突的定义
该代码创建 System.Guid 类型的别名(称为 Guid),并创建自己的类型(称为 Guid)并具有匹配的构造函数接口。随后,代码创建 Guid 类型的实例。要创建此实例,编译器必须在 Guid 的两个不同定义之间进行选择。当 using-alias 指令放置在命名空间元素之外时,编译器将选择本地命名空间内定义的 Guid 的本地定义,并完全忽略命名空间之外定义的 using-alias 指令。不幸的是,在阅读代码时这一点并不明显。
然而,当 using-alias 指令位于命名空间中时,编译器必须在同一命名空间中定义的两个不同的、冲突的 Guid 类型之间进行选择。这两种类型都提供了匹配的构造函数。编译器无法做出决定,因此它会标记编译器错误。
将 using-alias 指令放在命名空间之外是一种不好的做法,因为在这种情况下可能会导致混乱,在这种情况下,实际使用的类型版本并不明显。这可能会导致难以诊断的错误。
将 using-alias 指令放置在命名空间元素中可以消除这种错误来源。
将多个命名空间元素放置在一个文件中通常是一个坏主意,但是如果这样做,最好将所有 using 指令放置在每个命名空间元素中,而不是全局放置在文件顶部。这将严格限制命名空间,并且还有助于避免上述行为。
需要注意的是,当使用放置在命名空间之外的 using 指令编写代码时,在命名空间内移动这些指令时应小心,以确保这不会改变代码的语义。如上所述,将 using-alias 指令放置在命名空间元素内允许编译器以将指令放置在命名空间之外时不会发生的方式在冲突类型之间进行选择。
如何纠正违规行为
要修复违反此规则的问题,请移动命名空间元素内的所有 using 指令和 using-alias 指令。
According the to StyleCop Documentation:
SA1200: UsingDirectivesMustBePlacedWithinNamespace
Cause
A C# using directive is placed outside of a namespace element.
Rule Description
A violation of this rule occurs when a using directive or a using-alias directive is placed outside of a namespace element, unless the file does not contain any namespace elements.
For example, the following code would result in two violations of this rule.
The following code, however, would not result in any violations of this rule:
This code will compile cleanly, without any compiler errors. However, it is unclear which version of the Guid type is being allocated. If the using directive is moved inside of the namespace, as shown below, a compiler error will occur:
The code fails on the following compiler error, found on the line containing
Guid g = new Guid("hello");
CS0576: Namespace 'Microsoft.Sample' contains a definition conflicting with alias 'Guid'
The code creates an alias to the System.Guid type called Guid, and also creates its own type called Guid with a matching constructor interface. Later, the code creates an instance of the type Guid. To create this instance, the compiler must choose between the two different definitions of Guid. When the using-alias directive is placed outside of the namespace element, the compiler will choose the local definition of Guid defined within the local namespace, and completely ignore the using-alias directive defined outside of the namespace. This, unfortunately, is not obvious when reading the code.
When the using-alias directive is positioned within the namespace, however, the compiler has to choose between two different, conflicting Guid types both defined within the same namespace. Both of these types provide a matching constructor. The compiler is unable to make a decision, so it flags the compiler error.
Placing the using-alias directive outside of the namespace is a bad practice because it can lead to confusion in situations such as this, where it is not obvious which version of the type is actually being used. This can potentially lead to a bug which might be difficult to diagnose.
Placing using-alias directives within the namespace element eliminates this as a source of bugs.
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.
How to Fix Violations
To fix a violation of this rule, move all using directives and using-alias directives within the namespace element.
当您希望使用别名时,在命名空间内放置 using 语句会出现问题。该别名无法从早期的
using
语句中受益,并且必须是完全限定的。考虑:
与:
如果您有一个冗长的别名,如下所示(这就是我发现问题的方式),这可能会特别明显:
在命名空间内使用
using
语句,它突然变成:漂亮的。
There is an issue with placing using statements inside the namespace when you wish to use aliases. The alias doesn't benefit from the earlier
using
statements and has to be fully qualified.Consider:
versus:
This can be particularly pronounced if you have a long-winded alias such as the following (which is how I found the problem):
With
using
statements inside the namespace, it suddenly becomes:Not pretty.
我遇到的一个问题(其他答案中没有涵盖):
假设您有这些命名空间:
当您使用
using Something.Other
outside 命名空间Parent,它引用第一个(Something.Other)。但是,如果您在该命名空间声明中使用它,它会引用第二个命名空间 (Parent.Something.Other)!
有一个简单的解决方案:添加“
global::
”前缀:文档One wrinkle I ran into (that isn't covered in other answers):
Suppose you have these namespaces:
When you use
using Something.Other
outside of anamespace Parent
, it refers to the first one (Something.Other).However if you use it inside of that namespace declaration, it refers to the second one (Parent.Something.Other)!
There is a simple solution: add the "
global::
" prefix: docs我认为其他答案没有涵盖的另一个微妙之处是当您有一个具有相同名称的类和命名空间时。
当您在命名空间内进行导入时,它将找到该类。如果导入位于命名空间之外,则导入将被忽略,并且类和命名空间必须是完全限定的。
Another subtlety that I don't believe has been covered by the other answers is for when you have a class and namespace with the same name.
When you have the import inside the namespace then it will find the class. If the import is outside the namespace then the import will be ignored and the class and namespace have to be fully qualified.
正如 Jeppe Stig Nielsen 所说,这个帖子已经有了很好的答案,但我认为这个相当明显的微妙之处也值得一提。
在命名空间内指定的
using
指令可以缩短代码,因为它们不需要像在外部指定时那样完全限定。以下示例之所以有效,是因为类型
Foo
和Bar
都位于同一全局命名空间Outer
中。假设代码文件 Foo.cs:
和 Bar.cs:
可能会省略
using
指令中的外部命名空间,简而言之:As Jeppe Stig Nielsen said, this thread already has great answers, but I thought this rather obvious subtlety was worth mentioning too.
using
directives specified inside namespaces can make for shorter code since they don't need to be fully qualified as when they're specified on the outside.The following example works because the types
Foo
andBar
are both in the same global namespace,Outer
.Presume the code file Foo.cs:
And Bar.cs:
That may omit the outer namespace in the
using
directive, for short:尚未提及:
将
using
指令放入命名空间声明内部是众所周知的最佳编程实践的应用在尽可能小的范围内声明所有内容。如果最佳编程实践是您的第二天性,那么您会自动执行类似的操作。
这可能是将您的 using 指令放入名称空间声明中的最佳理由,无论其他地方提到的(边界)技术(边界)优点如何;就这么简单。
已经提到过,但也许更好地说明了:
将
using
指令放置在命名空间内可以避免不必要的重复,并使我们的声明更加简洁。这是不必要的简洁:
这是甜蜜且切题的:
Not already mentioned:
Placing the
using
directives inside the namespace declaration is an application of the well-known best programming practice of declaring everything in the smallest scope possible.If best programming practices are second nature to you, then you do things like that automatically.
This might be the best reason for putting your using directives inside the namespace declaration, regardless of (borderline) technical (borderline) merits mentioned elsewhere; It's as simple as that.
Already mentioned but perhaps better illustrated:
Placing
using
directives inside the namespace avoids unnecessary repetition and makes our declarations more terse.This is unnecessarily terse:
This is sweet and to the point:
答案中讨论了技术原因,我认为最终取决于个人喜好,因为差异并没有那么大,而且两者都需要权衡。 Visual Studio 用于创建 .cs 文件的默认模板使用命名空间之外的
using
指令,例如,可以通过添加来调整 stylecop 以检查命名空间之外的
using
指令项目文件根目录中的stylecop.json
文件包含以下内容:您可以在解决方案级别创建此配置文件,并将其作为“现有链接文件”添加到您的项目中,以便在所有项目之间共享配置你的项目也是如此。
The technical reasons are discussed in the answers and I think that it comes to the personal preferences in the end since the difference is not that big and there are tradeoffs for both of them. Visual Studio's default template for creating
.cs
files useusing
directives outside of namespaces e.g.One can adjust stylecop to check
using
directives outside of namespaces through addingstylecop.json
file in the root of the project file with the following:You can create this config file in solution level and add it to your projects as 'Existing Link File' to share the config across all of your projects too.
通常,外部
using
指令(例如 System 和 Microsoft 命名空间)应放置在namespace
指令的外部。它们是默认值,应在所有情况下应用除非另有说明。这应该包括您自己组织的任何不属于当前项目的内部库,或者引用同一项目中其他主命名空间的using
指令。任何引用当前项目和命名空间中其他模块的using
指令都应放置在namespace
指令内部。这有两个特定的功能:后一个原因很重要。这意味着引入不明确的参考问题会更困难,而这种问题可以通过不比重构代码更重要的更改来引入。也就是说,您将一种方法从一个文件移动到另一个文件,然后突然出现一个以前不存在的错误。通俗地说,“heisenbug”——历史上极难追踪。
As a rule, external
using
directives (System and Microsoft namespaces for example) should be placed outside thenamespace
directive. They are defaults that should be applied in all cases unless otherwise specified. This should include any of your own organization's internal libraries that are not part of the current project, orusing
directives that reference other primary namespaces in the same project. Anyusing
directives that reference other modules in the current project and namespace should be placed inside thenamespace
directive. This serves two specific functions:The latter reason is significant. It means that it's harder to introduce an ambiguous reference issue that can be introduced by a change no more significant than refactoring code. That is to say, you move a method from one file to another and suddenly a bug shows up that wasn't there before. Colloquially, a 'heisenbug' - historically fiendishly difficult to track down.
这取决于。
根据 StyleCop 的说法,里面。
据微软称,外部。
(只是想要一个真正简短的答案,我阅读了整个线程,但并不是每个人都有时间这样做)
It depends.
According to StyleCop, inside.
According to Microsoft, outside.
(Just wanted a real short answer, I read the entire thread, but not everyone has time for that)
虽然大多数答案倾向于将
using
指令放置在namespace
声明中,但有趣的是,微软在 C# 编码约定 另有说明:然而,在这篇文章中,他们规定相反:
While most answers lean towards placing
using
directives inside thenamespace
declaration, it is interesting to see that Microsoft's recommendation in C# Coding Conventions says otherwise:In this other post however, they prescribe the opposite:
如果源解决方案中使用的默认(即“引用”)应该位于命名空间之外,并且“新添加的引用”< /strong> 是一个很好的做法,您应该将其放在名称空间内。这是为了区分正在添加的引用。
It is a better practice if those default using i.e. "references" used in your source solution should be outside the namespaces and those that are "new added reference" is a good practice is you should put it inside the namespace. This is to distinguish what references are being added.