Class Initialize() in C#?

In Obj-c there is the static Initialize method which is called the first time that class is used, be it statically or by an instance. Anything like that in C#?

Jon Skeet
people
quotationmark

You can write a static constructor with the same syntax as a normal constructor, except with the static modifier (and no access modifiers):

public class Foo {
    static Foo() {
        // Code here
    }
}

Usually you don't need to do this, however - static constructors are there for initialization, which is normally fine to do just in static field initializers:

public class Foo {
    private static readonly SomeType SomeField = ...;
}

If you're using a static constructor to do more than initialize static fields, that's usually a design smell - but not always.

Note that the presence of a static constructor subtly affects the timing of type initialization, requiring it to be executed just prior to the first use - either when the first static member access, or before the first instance is created, whichever happens first.

people

See more on this question at Stackoverflow