【问题标题】:Does the location of the `using` directive make a difference in C#?`using` 指令的位置对 C# 有影响吗?
【发布时间】:2009-01-13 08:37:36
【问题描述】:

我今天尝试对现有代码进行一些格式化时出错。最初,代码在命名空间外声明了 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
}

我想知道这两种情况有什么区别? (import-style) using 指令的位置重要吗?

【问题讨论】:

    标签: c# syntax


    【解决方案1】:

    我以前认为没关系,但我总是使用命令完全限定。

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

    using-directive 的范围特别不包括其对等 using-directives。因此,peer using-directives 不会相互影响,并且它们的写入顺序无关紧要。

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

    【讨论】:

      【解决方案2】:

      是的,确实如此 - 在很小的程度上。范围/本地名称有一个边缘情况,请参阅Eric Lippert's blog

      举一个具体的例子(特定于别名的使用):

      using System;
      using Foo = Bar;
      public static class Bar {
          public  static void Test() { Console.WriteLine("outer"); }
      }
      namespace MyNamespace {
          //using Foo = Bar;
      
          public static class Bar {
              public static void Test() { Console.WriteLine("inner"); }
          }
          static class Program {
              static void Main() {
                  Foo.Test();
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        是的,我有一个working example 和一个类似的question (Namespace collisions)

        本质上,如果 using 指令是全局的(在命名空间之外)或在命名空间内部(当它具有更高优先级时),则名称查找的规则是不同的。

        【讨论】:

          【解决方案4】:

          这纯粹是信息性的,是声称它会影响程序集加载时间的回复之一;但实际上,程序集是在第一次使用它们的方法/类型被 JITted(或通过反射等访问)时加载的。

          这是一个例子,表明它没有区别;在任一位置使用 using 时,输出都是相同的:System.Core 加载为 Bar 得到 JITted:

          pre-Bar
          System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
          System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
          pre-Max
          Max: 5
          post-Max
          post-Bar
          

          示例(在命令行中运行,而不是在调试器中;最好在发布配置中):

          using System;
          //using System.Linq;
          namespace Foo {
              using System.Linq;
              class Program {
                  static Program() {
                      AppDomain.CurrentDomain.AssemblyLoad += (s, a) => {
                          Console.WriteLine(a.LoadedAssembly.FullName);
                      };
                  }
                  static void Main() {            
                      Console.WriteLine("pre-Bar");
                      Bar();
                      Console.WriteLine("post-Bar");
                      Console.ReadLine();
                  }
                  static void Bar() {
                      Console.WriteLine("pre-Max");
                      int[] data = { 1, 2, 3, 4, 5 };
                      Console.WriteLine("Max: " + Enumerable.Max(data));
                      Console.WriteLine("post-Max");
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2010-11-12
            • 1970-01-01
            • 2015-02-17
            • 2020-01-05
            • 1970-01-01
            • 2014-10-12
            • 1970-01-01
            • 2014-04-16
            • 2010-12-10
            相关资源
            最近更新 更多