gpt4 book ai didi

c# - `using` 指令的位置在 C# 中有什么不同吗?

转载 作者:太空狗 更新时间:2023-10-29 22:18:17 25 4
gpt4 key购买 nike

我今天在尝试对现有代码进行一些格式化时遇到错误。最初,代码在命名空间外声明了 using 指令:

using System.Collections.Generic;
namespace MyNamespace
{
using IntPair = KeyValuePair<int, int>;
}

当我试图在语句中插入 using 指令时(为了遵守 StyleCop 的规则),我在别名指令处遇到错误,我必须完全限定它:

namespace MyNamespace
{
using System.Collections.Generic;
//using IntPair = KeyValuePair<int, int>; // Error!
using IntPair = System.Collections.Generic.KeyValuePair<int, int>; // works
}

我想知道这两种情况有什么区别? (导入样式)using 指令的位置重要吗?

最佳答案

以前我觉得没关系,但我总是用命令来完全限定。

编辑:检查了 C# spec ,第 9.4 节说:

The scope of a using-directive specifically does not include its peer using-directives. Thus, peer using-directives do not affect each other, and the order in which they are written is insignificant.

因此,如果 System.Collections.Generic 和 KeyValuePair 被视为对等项,则 KeyValuePair 将忽略 System.Collections.Generic。

关于c# - `using` 指令的位置在 C# 中有什么不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/438336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com