Generic Static Constructors

suggest change

A static constructor on a generic type runs once for each closed type, that is, once for each distinct combination of type arguments.

class Animal<T>
{
    static Animal()
    {
        Console.WriteLine(typeof(T).FullName);
    }

	public static void Yawn() { }
}

Animal<Object>.Yawn();
Animal<String>.Yawn();

This will output:

System.Object System.String

See also How do static constructors for generic types work?

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents