Having static constructor, even just empty, changes the initialization flow.
in case we have a logger class just for logging
class Logger
{
public Logger(string text) => Console.WriteLine(text);
}
and a class with a static field with default initialization
class TestClass
{
public static Logger StaticLogger = new Logger("StaticLogger");
}
in the console app after we instantiate TestClass, "StaticLogger" is not written on the console window.
class Program
{
static void Main(string[] args)
{
new TestClass();
}
}
But if we add an empty static constructor in our TestClass
class TestClass
{
public static Logger StaticLogger = new Logger("StaticLogger");
static TestClass()
{
}
}
After instantiation of the TestClass, now the "StaticLogger" is written on the console window.
Why does static constructor change this behaviour?
I tested with .NET core 2.1