“使用”命名空间声明中的语句位置

发布于 2024-11-07 02:57:55 字数 792 浏览 0 评论 0原文

可能的重复:
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 技术交流群。

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

发布评论

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

评论(3

清风无影 2024-11-14 02:57:55

这里没有什么可关心的;事实上,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.

温柔一刀 2024-11-14 02:57:55

唯一的区别与范围界定有关。在第一种情况下,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.

满栀 2024-11-14 02:57:55

using 语句的位置
影响参考文献的范围
在文件内。

我想不出任何情况可以“保证”将其限制在较小的范围内。不过,我更愿意遵守惯例 - 或者详尽地评论 - 只是为了确保“下一个人”不必猜测我的意图。

也许此链接将详细回答您的问题

Location of the using statement
affects the scope of the references
within the 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

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