Consider the following class snippet with two static member variables:
public static class Foo
{
static string A = GetA(B);
static string B = "required for A";
...
Now, my understanding is that A
and B
will be initialized when they are accessed for the first time. However, when I executed a fully-realized version of the snippet above where A
was accessed before B
was initialized, it led to null
being passed in to GetA()
instead of "required for A"
. Why isn't the behaviour to start initializing A, then, when it's realized that B
is required to initialize A
, initialize B
, then return to finish the initialization of A
?
What are the general rules around this? Why does it behave this way? I've seen other questions that touch on this (When do static variables get initialized in C#?) but they don't answer this question exactly. What is the static variable initialization order in C#? talks primarily about how this works across classes, not within a single class (though Jon Skeet's addendum to his answer -- "By popular demand, here was my original answer when I thought the question was about the initialization order of static variables within a class:...." does answer this question, it's buried in a much longer answer).