C# - using语句的位置 [英] C# - Location of using statements

查看:181
本文介绍了C# - using语句的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一件事情我已经注意到了很多来回的就是using语句应放置在C#代码文件 - 无论是其最外层范围或一个命名空间内。据我了解,using语句的位置会影响文件中的引用的范围,但我不明白的是,为什么在大多数情况下,有人会永远使用他们自己的命名空间内声明希望。

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 {
		// ...
	}
}

这方面的一个例子被整个项目做似乎不必要是 ASP .NET MVC源

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

推荐答案

把使用,在文件的顶部是视觉的默认方式工作室。但是,推荐的方法是把命名空间里面的使用的语句。即使是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.

了StyleCop法则说:
放置多个命名空间的元素
在一个文件中一般是
坏主意,但如果当这是
做的,这是一个好主意,将所有
按指令在每个
命名空间的元素,而不是
全局在该文件的顶部。这种
就范围的命名空间紧密,和
也将有助于避免那种上述
行为。

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:

  • http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace
  • http://stackoverflow.com/questions/1071797/is-sa1200-all-using-directives-must-be-placed-inside-the-namespace-stylecop-pur
  • http://www.hanselman.com/blog/BackToBasicsDoNamespaceUsingDirectivesAffectAssemblyLoading.aspx
  • http://blogs.msdn.com/sourceanalysis/pages/sa1200-usingdirectivesmustbeplacedwithinnamespace.aspx

这篇关于C# - using语句的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆