静态构造函数c# 静态块java initallize oc
先看一道常见题目,以下代码的执行结果是什么?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class A
{ public static int X = B.Y + 1;
static void Main( string [] args)
{
Console.WriteLine(X);
}
} class B
{ public static int Z = 10;
public static int Y;
static B()
{
Console.WriteLine(Z);
Y = A.X + 1;
}
} |
一、定义
由名称可知,静态构造函数(也称为类型构造函数)包含“静态”和“构造函数”两个特点。第一个特点决定了它与静态函数类似,只能使用静态成员;第二个特点决定了它与构造函数类似,具有初始化作用,并且没有返回值。
与构造函数(针对实例对象)不同的是,静态构造函数(针对类)只执行一次,并且是在第一个实例对象创建前被调用,所以它可以用于那些只需要执行一次的操作;而且它不允许有public等修饰符,由程序自动调用,不能被外界调用。
总结:静态构造函数用于初始化任何静态数据,或者用于执行仅需执行一次的操作;在创建第一个实例对象或者引用任何静态变量之前,将自动调用静态构造函数。
特点:
1、静态构造函数既没有访问修饰符,也没有参数。
2、在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类。
3、无法直接调用静态构造函数。
4、在程序中,用户无法控制何时执行静态构造函数。
5、线程安全。
关于线程安全需要特别说明一下,由于程序可能在多线程环境下运行,也就是可能出现同时多个线程准备执行静态构造函数的情况。CLR确保这个过程是安全的,实际上调用静态构造函数的线程需要先获得一个互斥线程同步锁,如果有多个线程试图执行类型的静态构造函数,只有一个线程能获得该锁;获得锁的线程完成初始类型初始化操作,其它线程只能等待;当初始化完成,等待的线程被唤醒,然后发现静态构造函数已经被执行过,就不会再执行。
二、语法
1
2
3
4
5
6
|
public class StaticTester
{ static StaticTester()
{
}
} |
三、作用
用于初始化静态成员。有时候我们会从配置文件读取一些值作为静态变量,类似这样:
1
2
3
4
5
6
7
8
9
|
public class StaticTester
{ private static readonly string key = ConfigurationManager.AppSettings[ "key" ];
private static readonly string value = ConfigurationManager.AppSettings[ "value" ];
static StaticTester()
{
}
} |
如果要读取的配置信息比较多,而且要加入一些逻辑判断,那么可以这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class StaticTester
{ private static readonly string key;
private static readonly string value;
static StaticTester()
{
key = ConfigurationManager.AppSettings[ "key" ];
if ( string .IsNullOrEmpty(key))
{
throw new Exception( "key变量未被正确配置!" );
}
value = ConfigurationManager.AppSettings[ "value" ];
if ( string .IsNullOrEmpty(value))
{
throw new Exception( "value变量未被正确配置!" );
}
}
} |
四、执行顺序
1. 运行时,优先对静态变量进行初始化。
2. 如果有静态构造函数,那么在创建第一个实例对象或者引用任何静态变量之前,调用静态构造函数。
3. 其它操作。
父类--静态代码块
子类--静态代码块
父类--非静态代码块
父类--构造函数
子类--非静态代码块
子类--构造函数
子类--静态方法
C# static constructor[edit]
In C#, a static constructor is a static data initializer. Static constructors are also called class constructors. Since the actual method generated has the name .cctor they are often also called "cctors".[5][6]
Static constructors allow complex static variable initialization.[7] Static constructors are called implicitly when the class is first accessed. Any call to a class (static or constructor call), triggers the static constructor execution. Static constructors are thread safe and implement a singleton pattern. When used in a generic programmingclass, static constructors are called at every new generic instantiation one per type. Static variables are instantiated as well.
public class MyClass
{
private static int _A;
// Normal constructor
static MyClass()
{
_A = 32;
}
// Standard default constructor
public MyClass()
{
}
}