次のクラスがあるとします。
public class Foo {
static Foo() {
Console.WriteLine("Foo is being constructed");
}
}
public class Bar {
public void ReferenceFooAsGenericTypeParameter<T>() {
Console.WriteLine("Foo is being referenced as a generic type parameter");
}
}
public class SampleClass
{
public static void Main()
{
new Bar().ReferenceFooAsGenericTypeParameter<Foo>();
}
}
出力は
Foo はジェネリック型パラメーターとして参照されています
仕様によると、これは理にかなっています。
最初のインスタンスが作成される前、または静的メンバーが参照される前に、クラスを初期化するために静的コンストラクターが自動的に呼び出されます。
しかし、型がジェネリック型パラメーターとして参照されているときに静的コンストラクターが呼び出されないのはなぜでしょうか。