When is a Static Constructor Executed in C#?
In C#, a static constructor, also known as class constructor, is called when the class is accessed for the first time. This is in contrast to instance constructors, which are invoked when an instance of a class is created.
Explanation:
According to the Microsoft documentation on Static Constructors:
"A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced."
Therefore, the static constructor is not called when the assembly containing the class is first loaded. Instead, it is executed when the class is first encountered in the code, before any instances are created or static members are accessed.
The above is the detailed content of When Does a C# Static Constructor Get Executed?. For more information, please follow other related articles on the PHP Chinese website!