17.11 Static constructors

原创 2006年05月21日 02:53:00
A static constructor is a member that implements the actions required to
initialize a class. Static constructors are
declared using static-constructor-declarations:
attributesopt static-constructor-modifiers identifier ( )
externopt static
static externopt
A static-constructor-declaration may include a set of attributes (§24) and
an extern modifier (§17.5.7).
Chapter 17 Classes
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
[Example: The example
using System;
class Test
static void Main() {
class A
static A() {
Console.WriteLine("Init A");
public static void F() {
class B
static B() {
Console.WriteLine("Init B");
public static void F() {
must produce the output:
Init A
Init B
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
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]

Static Constructors

A static constructor is used to initialize any static data, or to perform a particular action that...
  • chuwachen
  • chuwachen
  • 2014年07月07日 18:17
  • 401

Aspose.Words 11月新版17.11发布 | 修复表格在特定条件下丢失

Aspose.Words 11月新版17.11发布,修复表格在特定环境下丢失的问题。
  • cocacola456
  • cocacola456
  • 2017年11月17日 16:36
  • 173

CSharp static constructors

C# and beforefieldinitImplementing the Singleton Pattern in C#Is the C# static constructor thread sa...
  • ChengZi
  • ChengZi
  • 2010年02月04日 10:26
  • 1043

8.7.10 Static constructors

A static constructor is a member that implements the actions required to initialize a class. Static ...
  • masterall
  • masterall
  • 2005年11月30日 19:12
  • 530


MyEclipse注册以及汉化方法  Subscriber:myeclipse8.5       Subscription Code:zLR8ZC-855550-68567156703100...
  • 2012年06月11日 20:44
  • 2749

Avoid non-default constructors in fragments

在使用Android Studio时Fragment类报 Avoid non-default constructors in fragments
  • yyqasg
  • yyqasg
  • 2014年11月13日 12:38
  • 2306

Autofuc None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructor

最近项目中使用Autofuc实现依赖注入时出现错误: None of the constructors found with 'Autofac.Core.Activators.Reflectio...
  • shenwenbin0106
  • shenwenbin0106
  • 2017年03月17日 16:02
  • 1530

understanding constructors

understanding constructors                  --How constructors differ from methods                  ...
  • majianan
  • majianan
  • 2007年04月20日 18:47
  • 818

C++:类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)

C++:类的语法错误  errorc 2533:constructors not allowed a return type(构造函数不允许返回一个类型)...
  • sky_will
  • sky_will
  • 2016年04月10日 09:44
  • 1892

C++三种class constructors

class A { public: A(int a) : a_(a) {} A(const A &a) : a_(a.a_) {} A &operator=(con...
  • u013034520
  • u013034520
  • 2017年11月03日 06:23
  • 149
您举报文章:17.11 Static constructors