How can I run a static initializer method in C# before the Main() method?
Simply do the initialization inside a static constructor for Foo
.
From the documentation:
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
There are static constructors in C# that you can use.
public static class Foo
{
// Class members...
static Foo(){
init();
// other stuff
}
internal static init()
{
// Do some initialization...
}
}
Move your code from an internal static
method to a static
constructor, like this:
public static class Foo
{
// Class members...
static Foo()
{
// Do some initialization...
}
}
This way, you are quite sure that the static constructor will be run on first mention of your Foo
class, whether it's a construction of an instance or access to a static member.