I have a set of static variables
static string A;
static string B;
static string C;
...
that I would like to initialize.
Now, I could do
static string A;
...
static string Z = InitializeAllVariables();
static void InitializeAllVariables()
{
     /// Initialize all my static variables
}
but that's not very elegant.
Is there a way to force InitializeAllVariables() to run on class load so that I don't need to explicitly call it through a static variable definition?
Thanks.
Use a static constructor.
public static class MyClass
{
    static string A;
    static string B;
    static string C;
    static MyClass()
    {
        A = "Hello";
        B = "World";
        C = "!";
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With