Is the “textual order” across partial classes formally defined?

后端 未结 2 902
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 06:34

Specifically, in relation to field initializers (in this case, static) - §17.11 in ECMA 334:

If a class contains any static fields with initializers,

相关标签:
2条回答
  • 2020-12-03 07:01

    Here's another snippet from the C# spec which, taken with your snippet, appears to settle that this is undefined behaviour:

    10.2.6 Members

    [...] The ordering of members within a type is rarely significant to C# code, but may be significant when interfacing with other languages and environments. In these cases, the ordering of members within a type declared in multiple parts is undefined.

    To be fair, it does say rarely significant, not insignificant :).

    0 讨论(0)
  • 2020-12-03 07:18

    I know this question is very old, but here is the correct part of the specification:

    ECMA-334 (C# Language Specification), Variable initializers, section 17.4.5:

    When there are field declarations in multiple partial type declarations for the same type, the order of the parts is unspecified. However, within each part the field initializers are executed in order.

    So the "textual order" in partial types is not undefined, rather it's partially defined. ;-)

    0 讨论(0)
提交回复
热议问题