Replies: 12 comments
-
It is not defined when static initialisation will take place. Indeed different runtimes do this at different times. The general trend has been for this to become increasingly lazy over time. |
Beta Was this translation helpful? Give feedback.
-
But if so, that sample is just wrong. There is no initalization dependency between A and B. |
Beta Was this translation helpful? Give feedback.
-
Can you add a link to that section please? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Static initialisation can definitely occur before Main, as this example will prove: using System;
public class C {
static int b = a + 5;
static int a;
public static void Main() {
a = 5;
WriteB();
}
private static void WriteB() => Console.WriteLine(b);
} This prints 5, not 10 |
Beta Was this translation helpful? Give feedback.
-
The reason for that sample is that when there is a static constructor, I think all static field initialisers are moved to the static constructor. The static constructor is guaranteed to be lazy, unlike static field initialisers. |
Beta Was this translation helpful? Give feedback.
-
No, that is no prove. As defined in 17.4.5.1 static field initialization is done in textual order, right after default initialization. In your sample a and b are default initialized to zero. Than b is initialized (0 + 5 => 5). |
Beta Was this translation helpful? Give feedback.
-
The relevant section of the spec is in static constructors, and is:
|
Beta Was this translation helpful? Give feedback.
-
That clearly happens before Main, or a would already be 5. |
Beta Was this translation helpful? Give feedback.
-
So executing a static constructor before instance creation or static member access is not allowed? Or is it just garanteed that static constructor executions is executed at least before those events? In the CLR this is not a problem because it can execute static initialization right after JITing. Afterwards no check is required to access static fields. |
Beta Was this translation helpful? Give feedback.
-
The indication of the spec is that it is required. Indeed that seems to be what the 2nd sample is trying to point out. This is a property of the CLR. I would open an issue on CoreCLR if you want that changed. |
Beta Was this translation helpful? Give feedback.
-
Are you meaning order of multiple static members in 1 class? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
According to "§17.4.5.1 static field initialization" static field initializers are executed after static default initialization followed by an optional static constructor execution.
It is garanteed that all initializers are executed before the static constructor gets executed and that static initialization must be done before first use.
The second sample in this section says that the output must be
My question:
Is it garanteed by the spec that static initialization is deferred to the first usage of a member? Is it not allowed to do static intialization for all types before executing Main?
If it is so, where it is defined in the spec?
Beta Was this translation helpful? Give feedback.
All reactions