C# Load a static method on class load

c#

Solution

Use a static constructor.

public static class MyClass
{
    static string A;
    static string B;
    static string C;

    static MyClass()
    {
        A = "Hello";
        B = "World";
        C = "!";
    }
}

Problem

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.

Original source