We recommend using Visual Studio 2017

CA2121: Static constructors should be private


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CA2121: Static constructors should be private.

|Breaking Change|Breaking|

A type has a static constructor that is not private.

A static constructor, also known as a class constructor, is used to initialize a type. The system calls the static constructor before the first instance of the type is created or any static members are referenced. The user has no control over when the static constructor is called. If a static constructor is not private, it can be called by code other than the system. Depending on the operations that are performed in the constructor, this can cause unexpected behavior.

This rule is enforced by the C# and Visual Basic .NET compilers.

Violations are typically caused by one of the following actions:

  • You defined a static constructor for your type and did not make it private.

  • The programming language compiler added a default static constructor to your type and did not make it private.

To fix the first kind of violation, make your static constructor private. To fix the second kind, add a private static constructor to your type.

Do not suppress these violations. If your software design requires an explicit call to a static constructor, it is likely that the design contains serious flaws and should be reviewed.