Se ci fosse stato class al post di struct, questo snippet:
using System;
struct P
{
static P()
{
Console.WriteLine("Initializing P");
}
}
class Test
{
static void Main()
{
P p = new P();
}
}
avrebbe stampato a console "Initializing P". Così invece, non stampa nulla! Dobbiamo quindi fare attenzione alla parola class in questo frammento di C# Language Specification (17.11):
"The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
- An instance of the class is created.
- Any of the static members of the class are referenced."
"The creation of the struct instance p using the parameterless default instance constructor P() does not trigger the execution of the static constructor of P", aggiunge Nicu Fruja.