Few weeks ago, I switched from Java to C#. Today, I had a weird behavior and I try to reproduce it in this simple sample. I\'m using a .net FW 4.
I have three classes: F
It is defined as text order - §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.
As an aside, this gets particularly interesting if you consider partial classes
, in which case : it is not defined.
If in doubt, move the initialization explicitly to a static constructor.
As for why; consider (note: these are just my own thoughts):
partial classes
issue, the full order itself is not strictly defined; so it cannot handle the general case - and again, covering a specific case (single class fragment etc) is back to the "thin veneer" (where it only warns for the obvious cases, but can't help with the non-trivial ones)