Given the trivial C# console application below, I would expect the output to be: NullCell = 1.23. Instead it outputs: NullCell = 0.0
This feels like a bug as C# commits to calling the static constructor on a class before an instance or a static method on the class is referenced.
I suspect the compiler is looking to the base class for the definition, rather than the derived class in at the point the value of the static NullCell is asked for. Given the static is inherited, and the derived class is being referenced I would have expected the static constructor on the derived class to have been called.
namespace StaticConstructorConsoleApp
{
public abstract class BaseClass
{
public static float NullCell;
}
public class DerivedClass : BaseClass
{
static DerivedClass()
{
NullCell = 1.23f;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"NullCell = {DerivedClass.NullCell}");
Console.ReadKey();
}
}
}