A static constructor is a member that implements the actions required to
initialize a class. Static constructors are
declared using static-constructor-declarations:
static-constructor-declaration:
attributesopt static-constructor-modifiers identifier ( )
static-constructor-body
static-constructor-modifiers:
externopt static
static externopt
static-constructor-body:
block
;
A static-constructor-declaration may include a set of attributes (§24) and
an extern modifier (§17.5.7).
Chapter 17 Classes
263
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 staticconstructor-
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 (§17.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 (§10.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.
[Example: 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");
}
}
class B
{
static B() {
Console.WriteLine("Init B");
}
public static void F() {
Console.WriteLine("B.F");
}
}
must produce the output:
Init A
A.F
Init B
B.F
because the execution of A’s static constructor is triggered by the call
to A.F, and the execution of B’s static
constructor is triggered by the call to B.F. end example]
It is possible to construct circular dependencies that allow static fields
with variable initializers to be observed in
their default value state.
[Example: The example
C# LANGUAGE SPECIFICATION
264
using System;
class A
{
public static int X;
static A() { X = B.Y + 1;}
}
class B
{
public static int Y = A.X + 1;
static B() {}
static void Main() {
Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y);
}
}
produces the output
X = 1, Y = 2
To execute the Main method, the system first runs the initializer for B.Y,
prior to class B’s static constructor.
Y’s initializer causes A’s static constructor to be run because the value
of A.X is referenced. The static constructor
of A in turn proceeds to compute the value of X, and in doing so fetches
the default value of Y, which is zero. A.X
is thus initialized to 1. The process of running A’s static field
initializers and static constructor then completes,
returning to the calculation of the initial value of Y, the result of which
becomes 2. end example]
分享到:
相关推荐
数据平面开发套件(DPDK [1] ,Data Plane Development Kit)是由6WIND,Intel等多家公司开发,主要基于Linux系统运行,用于快速数据包处理的函数库与驱动集合,可以极大提高数据处理性能和吞吐量,提高数据平面应用程序...
dpdk-tools说明,包括dpdk-devbind、dpdk-pdump、dpdk-pmdinfo、dpdk-procinfo等命令
Aspose.Words for Java 17.11。。。。。。。。。。。。
著名的PDF编程插件17.11版本,已解压,手动安装,支持Delphi 10.3 Rio Quick PDF Library for Windows ActiveX – ActiveX COM for Windows development supports C#, Visual C++, C++ Builder, Delphi, Visual ...
Aspose.Tasks-17.11.jar 内含lic校验文件,真实有效,放心使用
资产评估基础_总结17.11.3.doc
dpdk-17.11.2.tar.xz,源码下载。
DPVS的rpm安装包,可以解决重启不生效问题,希望能帮到大家
Frontline Protocol Analysis System 13.2 (17.11.15307.15525) for Windows 7 (32-bit & 64-bit), Windows 8.1 (32-bit & 64-bit), Windows 10 (32-bit & 64-bit)
Foxit.Quick.PDF.Library.v17.11.WinAll.Regged-FALLEN.rar Debunu Quick PDF Library
高级软考从2017年11月进行了改版,上传的文档中仅包括2017年11月改版以来到2019年05的真题及解析,下载后可直接打印。答案解析准确,详细,若需要2017年11月之前的可直接在我的博客下方留下邮箱,我会把其他年份的...
Python零基础10天进阶班【17网站开发基础(下)】
A5下载站向大家推荐一款专为中小学班主任设计的教学辅助软件——星空班主任办公系统。星空班主任办公系统功能强大,能够实现包括分析学生考试成绩,打印学生登分名册,记载学生考勤情况,点写学生评语,打印学生通知...
软件介绍: 电阻衰减器计算工具,可以计算单端和差分Pi型和Y型衰减器电阻值,另外可以根据电阻值反算衰减器值和驻波,电阻衰减器计算工具支持电路拓扑:单端PI型、单端T型、差分PI型、差分-T型。...
精品教育教学资料
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
宝藏来源 2013年Spring,CS 17.11 Java编程@ SRJC