struct TestStruct
{
static TestStruct()
{
Console.WriteLine("TestStruct");
}
}
When static parameterless constructor is called in structure.
struct TestStruct
{
static TestStruct()
{
Console.WriteLine("TestStruct");
}
}
When static parameterless constructor is called in structure.
To invoke it explicitly but safely (once-only, without needing to worry about whether it exists, etc):
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(
type.TypeHandle);
However:
When static parameterless constructor is called in structure.
If you mean "when will the runtime execute it" - the only safe answer is when it needs to - the exact details are very complex, and change between runtimes. It would be unwise to depend on the exact timing of this. However, it is guaranteed to execute before you (for example) attempt to access any static fields.
Static constructors are called automatically by the runtime.
The specification details when they are called:
11.3.10 Static constructors
The execution of a static constructor for a struct type is triggered by the first of the following events to occur within an application domain:
A static member of the struct type is referenced. An explicitly declared constructor of the struct type is called. The creation of default values (§11.3.4) of struct types does not trigger the static constructor. (An example of this is the initial value of elements in an array.)