Wednesday, 15 February 2012

c# - Static field initialization order with partial classes -


Is there a way to apply static field initialization commands in partial classes? I have this in the HelloWald 1cc:

  partial category helowold {stable readonly string [A] new [] {"Hello World"}; }  

I have elsewhere in HelloWorld2.cs:

  partial category HelloWorld {static readonly string b = a [0]; }  

If B is started before, then it is fine but if B is initialized first, then it throws one. It is possible to use a stable manufacturer in a healthy way, but I am curious that if the regional class is in separate files in the same fractional class, there is no way to force or estimate the initialization order.

When the field is present in the same file, the text command defines the execution of its initialization :

10.5.5.1 convertible initial - static field initialization

Static field variables of a class correspond to the sequence of initial tasks In the class declaration, which they appear in order if a stable producer (§10.12) is in the class Ujud, steady field is already executing the initial execution of the static constructor immediately. Otherwise, the static field initiator is executed at an implementation-dependent time before the first use of a stable area of ​​that class.

However, in the case of areas declared in different files, partial classes, the order is unspecified:

10.2.6 Partial type - Member

The sequence of members within a type is rarely the C # code, but can be important when interfacing with other languages ​​and the environment In these cases, in many parts The order of the members within a given declaration is undefined .


No comments:

Post a Comment