Static Constructors
Where instance constructors take on the task of initializing a new object instance, the role of static constructors (sometimes referred to as type initializers) is to initialize a type’s static state. Although the runtime provides type initializers for more than just classes and structs, C# restricts their declaration to those categories of types (mainly because of their limited use of other types).
A static constructor looks like an instance constructor but is declaring using the static keyword, doesn’t have an access modifier, and can’t take any parameters. Just like any other static member, it can only access other static members and is primarily used to assign the type’s static fields. In fact, static field initializers ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access