这两个声明有什么区别?

前端之家收集整理的这篇文章主要介绍了这两个声明有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
鉴于此声明:
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;如果你真的想.)

我个人坚持:

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

原文链接:https://www.f2er.com/csharp/95336.html

猜你在找的C#相关文章