Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Итак: статический конструктор вызывается перед доступом к любому члену класса, за исключением констант.
MapBuilder.BuildMode = BuildMode.Default;
MapBuilder.Build();
var mb = new MapBuilder();
mb.BuildMode = BuildMode.Default;
DoAnythingWithMapBuilder(mb); // внутри этого метода может еще раз вызваться Build() или измениться свойство BuildMode
mb.Build();
MapBuilder.Build(BuildMode.Default);
Если попробовать выполнить приведённый ниже код, то можно убедиться в верности следующего утверждения: статический конструктор вызывается перед доступом к любому члену класса.
10.5.5.1 Static field initialization The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class
Статика в C#