加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

这两个声明有什么区别?

发布时间:2020-12-15 06:32:30 所属栏目:百科 来源:网络整理
导读:鉴于此声明: using System;using System.Collections;using System.Collections.Generic;namespace AProject.Helpers{ public static class AClass { 和这个声明 namespace AProject.Helpers{ using System; using System.Collections; using System.Collec
鉴于此声明:
using System;
using System.Collections;
using System.Collections.Generic;

namespace AProject.Helpers
{
    public static class AClass
    {

和这个声明

namespace AProject.Helpers
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public static class AClass
    {

他们之间有什么区别吗?或者只是编码风格的区别?

我一直用来宣布我的课程像第一个,但最近注意到,微软uses the second.

解决方法

在后一版本中,using指令仅适用于命名空间声明.

在大多数情况下,您只能有一个命名空间声明:

// Using directives
...
namespace X
{
    // Maybe more using directives
    // Code
}
// End of file

主要区别是如果您在同一个文件中有多个命名空间:

// Using directives
...
namespace X
{
    // Maybe more using directives
    // Code
}

namespace Y
{
    // Maybe more using directives
    // Code
}
// End of file

在这种情况下,namespace X声明中的using指令不影响命名空间Y声明中的代码,反之亦然.

然而,这并不是唯一的区别 – 即使只有一个命名空间声明,也可以影响代码的subtle case which Eric Lippert points out. (基本上,如果你使用Foo编写;在命名空间X声明里面,还有一个名字空间X.Foo以及Foo,行为发生变化,这可以使用命名空间别名进行修复,例如使用global :: Foo;如果你真的想.)

我个人坚持:

>每个文件一个命名空间声明(通常每个文件一个顶级类型)>在命名空间声明之外使用指令

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读