✅ ✅ 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
reflectronic9mo ago
when are you expecting it to be called
Will Pittenger
Will Pittenger9mo ago
Well, before the instance constructor.
reflectronic
reflectronic9mo ago
uh, can you try and reduce the code sample
MODiX
MODiX9mo 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 Pittenger9mo ago
It normally works for me. But isn't know.
reflectronic
reflectronic9mo ago
does this X actually have the words static X() or do you just have static int Z = ...;
Will Pittenger
Will Pittenger9mo ago
It does.
reflectronic
reflectronic9mo 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 Pittenger9mo ago
Yes. It's up to date.
reflectronic
reflectronic9mo 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
Moods9mo 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 Pittenger9mo ago
Not really. I don't understand it.
Moods
Moods9mo 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 Pittenger9mo ago
I was there a few minutes ago.
Moods
Moods9mo 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 Pittenger9mo 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
Moods9mo 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
reflectronic9mo 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
Moods9mo ago
Curious, if it turns out to work, what could lead to the constructor not running
Will Pittenger
Will Pittenger9mo ago
That doesn't take a System.Type. It takes a System.RuntimeTypeHandle.
reflectronic
reflectronic9mo ago
typeof(X).TypeHandle yeah
Will Pittenger
Will Pittenger9mo 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
333fred9mo 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 Pittenger9mo ago
And I forgot that.
333fred
333fred9mo ago
Right. Just making sure the reason was clear, because your wording wasn't quite right 🙂
Accord
Accord9mo 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.
Want results from more Discord servers?
Add your server
More Posts
❔ NewtownSoft.JSON and classes that need references to its ownerI have two classes. Class A owns instances of Class B. But each instance of Class B needs a refere❔ Is it correct to use a single instance of Azure Front Door for different types of resources?I have an AFD profile in a resource group which was intended to be used to server Web APIs and it ha✅ DB context injection not workinghi, i have app like this - ```cs DotNetEnv.Env.Load("../build/"); var builder = WebApplication.Crea❔ Trying to find a way so it shows the sum of all the numbers entered and the also the average.```cs Modifier votre code Q2 afin de calculer/afficher la somme et ❔ What would be the best way to dynamicly cut in a string>So for example I have this: "C:\Users\USERNAME\Documents\Paradox Interactive\Hearts of Iron IV\mod\H❔ Reloading MainWindow after some changesHello guys, im currently working on a project to improve my C#/programming skills. I have following❔ need some help with a microsoft visual studio 2022 project im doinghttps://dotnetfiddle.net/ got my fiddle here, just nothing aint working and im lost rnDoesnt have the permision to save the fileSo I have been working on a Window Application and start working on a way to save the settings in a ✅ Project A sees one class in Project B, but not both (C#)I have two C# projects. In one, I declare two classes. One just derives from the other and adds so✅ Can the NewtonSoft JSON package conditionally create classes when parse?I have a situation where a field in a class will sometimes have a string, but other times have a cla