✅ ✅ Static constructor for C# class not being called

I declared a class, here X which has a normal constructor and a static constructor. The static constructor isn't being called leaving some critical steps not done. What's going on?
C#
class X
{
public X()
{
// The static constructor wasn't called here
}
static X()
{
// Critical step that must be done.
}
}
C#
class X
{
public X()
{
// The static constructor wasn't called here
}
static X()
{
// Critical step that must be done.
}
}
26 Replies
reflectronic
reflectronic16mo ago
when are you expecting it to be called
Will Pittenger
Will PittengerOP16mo ago
Well, before the instance constructor.
reflectronic
reflectronic16mo ago
uh, can you try and reduce the code sample
MODiX
MODiX16mo ago
reflectronic
REPL Result: Success
class X
{
static X()
{
Console.WriteLine("Static");
}

public X()
{
Console.WriteLine("Constructor");
}
}

new X();
class X
{
static X()
{
Console.WriteLine("Static");
}

public X()
{
Console.WriteLine("Constructor");
}
}

new X();
Console Output
Static
Constructor
Static
Constructor
Compile: 608.715ms | Execution: 81.595ms | React with ❌ to remove this embed.
Will Pittenger
Will PittengerOP16mo ago
It normally works for me. But isn't know.
reflectronic
reflectronic16mo ago
does this X actually have the words static X() or do you just have static int Z = ...;
Will Pittenger
Will PittengerOP16mo ago
It does.
reflectronic
reflectronic16mo ago
i am not sure what to say then like, are you sure you are running the up-to-date version of your code
Will Pittenger
Will PittengerOP16mo ago
Yes. It's up to date.
reflectronic
reflectronic16mo ago
because this is how it's supposed to work and there is no reason it would not be working this way without the specific code there is not really much that can be done to investigate
Moods
Moods16mo ago
You don’t really have any control on when the static constructor is called but it’s sure to be called before the instance constructor Can you provide more information if possible
Will Pittenger
Will PittengerOP16mo ago
Not really. I don't understand it.
Moods
Moods16mo ago
Static Constructors - C# Programming Guide - C#
A static constructor in C# initializes static data or performs an action done only once. It runs before the first instance is created or static members are referenced.
Will Pittenger
Will PittengerOP16mo ago
I was there a few minutes ago.
Moods
Moods16mo ago
Okay let me see In what way is the static constructor not being called affecting the instance constructor You said a critical step isn’t being done
Will Pittenger
Will PittengerOP16mo ago
There's an uninitialized static field. It can't be done with a simple static initializer as it has to check if an interface is implemented.
Moods
Moods16mo ago
Well I’m just thinking since it can’t be an issue with the static constructor not being called it could be an issue with the code inside
reflectronic
reflectronic16mo ago
what happens if you do RuntimeHelpers.RunClassConstructor(typeof(X)) before the method this will absolutely 100% guarantee that it runs if it still does not work, then it is not a problem with the constructor not running
Moods
Moods16mo ago
Curious, if it turns out to work, what could lead to the constructor not running
Will Pittenger
Will PittengerOP16mo ago
That doesn't take a System.Type. It takes a System.RuntimeTypeHandle.
reflectronic
reflectronic16mo ago
typeof(X).TypeHandle yeah
Will Pittenger
Will PittengerOP16mo ago
I figured it out. The problem was while the field I needed set up was initialized in the static constructor, the only instance is declared as a static instance in the same class. That did use a static initializer which was running before the static constructor. Ugh. So that caused the instance constructor to be called before the static constructor.
333fred
333fred16mo ago
Not quite the right description. Just like instance constructors, static constructors run all static field and property initializers before entering the main body of the static constructor. The static constructor is the thing that runs that field initializer, and it so happens that you called the instance constructor from that static field initializer. In general, be it static or instance, if you depend on ordering, do everything in the constructor body, not in initializers
Will Pittenger
Will PittengerOP16mo ago
And I forgot that.
333fred
333fred16mo ago
Right. Just making sure the reason was clear, because your wording wasn't quite right 🙂
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?