How can I run a static constructor?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, you do not normally call a static constructor yourself. The runtime runs it automatically before the type is first used, which is exactly what makes static initialization safe and predictable.
What a Static Constructor Does
A static constructor initializes a type, not an instance. It runs once per type in the application domain before:
- the first instance is created
- the first static member is accessed
A typical example looks like this:
The first time SettingsCache.Mode is accessed, the runtime ensures the static constructor has completed.
The Normal Answer: Trigger Type Initialization Indirectly
If your goal is “make sure the static constructor runs now,” the normal approach is to access a static member or create an instance. You do not call the constructor as a method because the language intentionally forbids that.
That line forces the type to initialize if it has not already done so.
This behavior matters for thread safety. The runtime guarantees that type initialization happens once, and that other threads do not observe the type before initialization finishes.
If You Truly Need to Force It
There is a specialized runtime API for forcing type initialization: RuntimeHelpers.RunClassConstructor. This is rarely needed, but it exists for infrastructure code, plugin loading, test setup, or other advanced scenarios.
This instructs the runtime to run the type initializer if it has not already run. If it already ran, nothing happens.
Use this carefully. If you feel the need to call it from ordinary application code, the design may be too dependent on hidden initialization.
Prefer Explicit Initialization for Application Logic
A lot of “how do I run the static constructor” questions are really design questions. Static constructors are useful for lightweight one-time setup, but they are a poor place for expensive I/O, network calls, or code that can fail for business reasons.
Instead of hiding important startup behavior in a static constructor, consider an explicit initialization method.
This approach is easier to test and easier to reason about. The caller controls when initialization happens and what inputs it uses.
What Happens When a Static Constructor Throws
If a static constructor throws an exception, the type becomes unusable for that application run and the runtime wraps the failure in TypeInitializationException.
Any later access to BrokenType can keep failing because type initialization never completed successfully. That is another reason to keep static constructors small and deterministic.
Common Pitfalls
The first pitfall is trying to declare the static constructor public and invoke it directly. C# does not allow that. Static constructors have no access modifier and no parameters.
Another mistake is using them for work that depends on external state, such as database connections or service calls. If that work fails, the entire type can remain broken.
A third mistake is assuming initialization order across unrelated types. The runtime guarantees a single type’s initialization semantics, but it does not promise a global ordering beyond actual usage dependencies.
Finally, if you need custom initialization inputs, a static constructor is probably the wrong tool. Use an explicit startup method or dependency injection instead.
Summary
- You normally do not call a static constructor directly in C#.
- The runtime runs it automatically before the type is first used.
- Accessing a static member is the ordinary way to trigger it.
- '
RuntimeHelpers.RunClassConstructorexists for advanced cases that must force initialization.' - Keep static constructors small, deterministic, and free of heavy application logic.

