首页 > 科技 >

C井静态构造函数何时执行?

2018-07-03 09:24:59 网络整理 阅读:200 评论:0

上篇说到了如果一个类有静态成员但没初始化,系统将不产生一个内联的静态构造函数,如果定义了静态成员且初始化了,编译器将生成一个内联的构造函数(当然了你没有显示定义静态构造函数的情况下)今天我要说的是编译器什么时候应该去调用静态构造函数

C井静态构造函数何时执行?

上面我红框,框起来的static 有与没有对程序的结果及执行顺序有很大的影响(相传是一道C井面试题 )。有static 打印结果2, 1 程序将在调用WriteLine时先执行A的构造,再B的构造; 没有static 时将打印1,2 程序会在进入Main函数之前先调用B的构造。我在网上也找了很多相关方面的资料和自己的一些代码测试,主要意思是如果没有显示定义构造函数,初始代码将在Main函数之前初始化,到底是在Main之前的哪一个语句编译器没有规定, 如果显示定义了静态构造函数, 程序将在你要创建类实例或引用静态字段或方法前调用。我也看了Jeffrey Richter 写的 CLR Via C Sharp 这本书关于这方面的一些讲解,一般来说他写的书还是有点深入的,我看的是英文版本,在这本书里他提到了静态构造函数对程序情能的影响 (在显示定义了构造函数时,,静态构造函数的代码将在初始化时内嵌到你的代码中, 所以在一个很大的循环里的话将影响性能, Jeffrey Richter在书中举了一个例子)下面是我从书中摘录的部分,也推荐大家去看一下这本书。

In the previous section, I mentioned that calling a type constructor is a tricky thing. And I explained some of the trickiness about it: the JIT compiler has to decide whether to emit the code to call it, and the CLR ensures that calls to it are thread-safe. As it turns out, this is the just the beginning of the tricky stuff. There is more about this that is performance related. As discussed already, when compiling a method, the JIT compiler determines whether it must emit a call to execute a type constructor into the method. If the JIT compiler decides to emit the call, it must decide where it should emit the call. There are two possibilities here:

相关文章