【问题标题】:Does the order of extern and static matter?外部和静态的顺序重要吗?
【发布时间】:2013-04-10 16:12:26
【问题描述】:

比较两个文件(旧的和新的),我看到:

private extern static void SipShowIM(uint dwFlag);

...在旧文件中,并且:

private static extern void SipShowIM(uint dwFlag);

...在新文件中。

我不知道他们为什么变了;哪个先出现重要吗,外部还是静态?

更新

Resharper 一定是这样做的,因为我知道我没有(直接)这样做,但这是 old 之间的另一个区别:

public volatile static bool ProcessCommands = true;

...和新的:

public static volatile bool ProcessCommands = true;

【问题讨论】:

  • 你注意到行为的不同了吗?
  • 不,但我会注意到什么不同?昨天在伊朗/巴基斯坦发生地震时,我没有注意到任何不同,但这并不意味着它没有影响任何事情。

标签: c# static-methods .net-1.0


【解决方案1】:

不,根据 C# 规范,所有方法修饰符的顺序都是等价的。 4.0 版,B.2.7 节,第 493 页:

方法修饰符:
方法修饰符
方法修饰符 方法修饰符

方法修饰符:

公开
受保护
内部
私人
静态
虚拟
密封
覆盖
摘要
外部

这显然不是静态构造函数,但在第 497 页(仍然是第 B.2.7 节),两个命令都被明确调用:

静态构造器修饰符:
外部opt 静态
静态外部opt

【讨论】:

    【解决方案2】:

    方法修饰符的顺序无关紧要。然而,它通常写成static extern

    StyleCop 之类的工具对此表示不满:SA1206: The 'static' keyword must come before the 'other' keyword in the element declaration. 这只是编码风格的问题。

    【讨论】:

      【解决方案3】:

      ,我不相信这两种用法有区别。我只考虑 MSDN 页面的代码,我尝试了两种方式(extern staticstatic extern),这两种代码生成相同的 IL 代码。

      .method public hidebysig static int32  Main(string[] args) cil managed
      {
        .entrypoint
        // Code size       41 (0x29)
        .maxstack  4
        .locals init ([0] string myString,
                 [1] int32 CS$1$0000)
        IL_0000:  nop
        IL_0001:  ldstr      "Enter your message: "
        IL_0006:  call       void [mscorlib]System.Console::Write(string)
        IL_000b:  nop
        IL_000c:  call       string [mscorlib]System.Console::ReadLine()
        IL_0011:  stloc.0
        IL_0012:  ldc.i4.0
        IL_0013:  call       native int [mscorlib]System.IntPtr::op_Explicit(int32)
        IL_0018:  ldloc.0
        IL_0019:  ldstr      "My Message Box"
        IL_001e:  ldc.i4.0
        IL_001f:  call       int32 ProgramConsole.Program::MessageBox(native int,
                                                                      string,
                                                                      string,
                                                                      int32)
        IL_0024:  stloc.1
        IL_0025:  br.s       IL_0027
        IL_0027:  ldloc.1
        IL_0028:  ret
      } // end of method Program::Main
      

      所以,我的钱是 NO

      【讨论】:

        【解决方案4】:

        不,这些关键字的顺序无关紧要。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-02
          • 2016-10-24
          • 1970-01-01
          • 2016-08-09
          • 1970-01-01
          • 2014-08-06
          • 2018-01-09
          • 1970-01-01
          相关资源
          最近更新 更多