X hits on this document

1026 views

0 shares

0 downloads

0 comments

269 / 396

C# LANGUAGE SPECIFICATION

static-constructor-modifiers: externopt   static static   externopt

static-constructor-body: block ;

A static-constructor-declaration may include a set of attributes (§‎17) and an extern modifier (§‎10.5.7).

The identifier of a static-constructor-declaration must name the class in which the static constructor is declared. If any other name is specified, a compile-time error occurs.

When a static constructor declaration includes an extern modifier, the static constructor is said to be an external static constructor. Because an external static constructor declaration provides no actual implementation, its static-constructor-body consists of a semicolon. For all other static constructor declarations, the static-constructor-body consists of a block which specifies the statements to execute in order to initialize the class. This corresponds exactly to the method-body of a static method with a void return type (§‎10.5.8).

Static constructors are not inherited, and cannot be called directly.

The static constructor for a class executes at most once in a given application domain. 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.

If a class contains the Main method (§‎3.1) in which execution begins, the static constructor for that class executes before the Main method is called. If a class contains any static fields with initializers, those initializers are executed in textual order immediately prior to executing the static constructor.

The example

using System;

class Test { static void Main() { A.F(); B.F(); } }

class A { static A() { Console.WriteLine("Init A"); } public static void F() { Console.WriteLine("A.F"); } }

256Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1026
Page views1026
Page last viewedSat Dec 10 01:52:09 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments