static Constructors

As opposed to an instance constructor, a static constructor of a class cannot be called explicitly in the source code. Instead, it is called automatically by the runtime sometime between the start of the program and the instantiation of the first object of the class. The static constructor is declared by writing the static keyword in front of the name of the class and by ending the line with empty parentheses (see Syntax Box 13.3). The statements are positioned in a block after this header.

There are a couple of notable differences between the declaration of a static constructor and an instance constructor. First, because the static constructor can only be accessed by the runtime, it makes no sense to specify an access modifier. ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.