Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c# - Is the "textual order" across partial classes formally defined?

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

If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor.

Now, if we have multiple partial classes in separate files, is that order determined anywhere? My gut says "not formally defined, but probably relates to the order included in the csproj, or the order noted to csc". Is this correct?

(and yes, I realise it would be better to avoid the ambiguity completely - probably by moving all the initialization to a static constructor).


For example, if I have a.cs:

using System;
partial class Program
{
    private static int Foo = Write("Foo");
    static int Write(string name)
    {
        Console.WriteLine(name);
        return 0;
    }
    static void Main()
    {
        Console.WriteLine("[press any key]");
        Console.ReadLine();
    }
}

and b.cs:

partial class Program
{
    private static int Bar = Write("Bar");
}

and:

<Compile Include="a.cs" />
<Compile Include="b.cs" />

then this is Foo then Bar; if, however, this is:

<Compile Include="b.cs" />
<Compile Include="a.cs" />

then it is Bar then Foo. This supports the observation, but does not state it strongly. §8.7.13 (Partial type declarations) makes no comment on the order when combining partial classes. So; is there anything stronger we can say here, either from the C# language spec or from the tooling documentation?

Additionally, it behaves similarly with csc a.cs b.cs vs csc b.cs a.cs.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...