c# - Static field initialization order with partial classes -


is there way force static field initialization order in partial classes? let's in helloworld1.cs have:

partial class helloworld {   static readonly string[] = new[] { "hello world" }; } 

elsewhere in helloworld2.cs have:

partial class helloworld {   static readonly string b = a[0]; } 

if initialized before b fine if b initialized before throws an. healthy way use static constructor i'm curious if there's way force or predict initialization order when fields classes in different files of same partial class.

when fields present in same file, textual order defines execution of initialization:

10.5.5.1 variable initializers - static field initialization

the static field variable initializers of class correspond sequence of assignments executed in textual order in appear in class declaration. if static constructor (§10.12) exists in class, execution of static field initializers occurs prior executing static constructor. otherwise, static field initializers executed @ implementation-dependent time prior first use of static field of class.

however, in case of fields declared in different files of partial classes, order undefined:

10.2.6 partial types - members

the ordering of members within type significant c# code, may significant when interfacing other languages , environments. in these cases, ordering of members within type declared in multiple parts undefined.

from c# language specification.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -