1

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();
        }
    }
}
Raymond Wilson
  • 217
  • 2
  • 6
  • 5
    I think you're technically accessing a static field belonging to the base class, so the derived class is never accessed. – ProgrammingLlama Apr 06 '20 at 04:45
  • 2
    See marked duplicate. Static constructors need not be called until an instance of the class is created, or you access a static member _of that class_. You haven't accessed a static member of the `DerivedClass` class, nor created an instance of it, so the static constructor need not be called. – Peter Duniho Apr 06 '20 at 04:49
  • 4
    And as an additional tip: it's highly unlikely you'll ever see a compiler bug in the wild, never mind in the area of such a basic language feature. The moment you start thinking the reason you're code's doing something unexpected is because of a compiler bug, that's the moment you're sunk. – Peter Duniho Apr 06 '20 at 04:51
  • Also, it is easy to force the static constructor on Derived to be called and if you do that you'll see that NullCell gets initialized as expected. – Brian Rasmussen Apr 06 '20 at 04:53
  • Peter: Regards compiler bugs, I agree. To date I have found four, so I don't completely discount it :) This is probably a fine point of semantics and expectation. I can see why the code is behaving as it does, but that wasn't expected as the static member is intrinsically a (shared) member of the derived class through that derivation and does not require the base class namespace to be used to access it. – Raymond Wilson Apr 06 '20 at 04:59
  • Brian: Yes, it's trivial to force the constructor to be called, so it's not a question of it being hard to work around, and I have no concerns with it behaving under those conditions. I ran into this because it broke some unit tests in a very unexpected way that is pretty transparent in that it compiles but misbehaves in a subtle way. – Raymond Wilson Apr 06 '20 at 05:02
  • `but that wasn't expected` Expectations are defined by the compiler, not the programmer alas. Confusing - yes. Compiler bug? Definitely not. – mjwills Apr 06 '20 at 05:42

0 Answers0