✅ ✅ 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?
26 Replies
when are you expecting it to be called
Well, before the instance constructor.
uh, can you try and reduce the code sample
reflectronic
REPL Result: Success
Console Output
Compile: 608.715ms | Execution: 81.595ms | React with ❌ to remove this embed.
It normally works for me. But isn't know.
does this
X
actually have the words static X()
or do you just have static int Z = ...;
It does.
i am not sure what to say then
like, are you sure you are running the up-to-date version of your code
Yes. It's up to date.
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
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
Not really. I don't understand it.
Hmmm, well in any case I hope this helps https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
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.
I was there a few minutes 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
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.
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
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 runningCurious, if it turns out to work, what could lead to the constructor not running
That doesn't take a
System.Type
. It takes a System.RuntimeTypeHandle
.typeof(X).TypeHandle
yeahI 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.
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
And I forgot that.
Right. Just making sure the reason was clear, because your wording wasn't quite right 🙂
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.