“使用”命名空间声明中的语句位置
可能的重复:
Usings应该在命名空间内部还是外部
我支持一些代码异常地将其所有 using 语句都包含在名称空间声明中。这让我有点不舒服,但我不知道这是否值得担心。除了违反惯例之外,我想不出还有什么直接问题。
有什么问题吗:
namespace myProject.controls
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
而不是更广泛使用的:
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace myProject.controls
{
Possible Duplicate:
Should Usings be inside or outside the namespace
I'm supporting some code that, unusually, has all its using statements contained within the namespace declaration. It makes me a bit uncomfortable but I have no idea if this should be anything to be concerned about or not. I can't think of an immediate problem other than it being counter to usual convention.
Is there anything wrong with:
namespace myProject.controls
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
Instead of the more widely-used:
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace myProject.controls
{
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里没有什么可关心的;事实上,StyleCop 默认推荐第一个示例(在命名空间声明内使用
using
语句)。There's nothing to be concerned with here; in fact, the first example (with the
using
statements inside the namespace declaration) is recommended by default by StyleCop.唯一的区别与范围界定有关。在第一种情况下,using 声明的范围位于名称空间声明内,并且在名称空间声明之外(例如同一文件中的其他名称空间声明中)不可用。在第二种情况下,声明在整个文件的范围内。
The only difference has to do with scoping. In the first case, the using declarations are scoped within the namespace declaration, and are not available outside it, in other namespace declarations in the same file, for example. In the second case, the declarations are in scope for the whole file.
我想不出任何情况可以“保证”将其限制在较小的范围内。不过,我更愿意遵守惯例 - 或者详尽地评论 - 只是为了确保“下一个人”不必猜测我的意图。
也许此链接将详细回答您的问题
I cannot think of any situation that would 'warrant' limiting it withing a smaller scope. Though, I would prefer sticking to conventions - or commenting exhaustively - , just to make sure 'next person' does not have to guess my intentions.
Probably this link will answers your question in detail