首先让我们认识什么是,当字段被标记为beforefieldinit类型时,该字段初始化可以发生在任何时候任何字段被引用之前。这句话听起了有点别扭,接下来让我们通过具体的例子介绍。

/// <summary>
/// Defines a test class.
/// </summary>
class Test
{
public static string x = EchoAndReturn("In type initializer"); public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}

上面我们定义了一个包含静态字段和方法的类Test,但要注意我们并没有定义静态的构造函数。

图3 Test类的IL代码

class Test
{
public static string x = EchoAndReturn("In type initializer"); // Defines a parameterless constructor.
static Test()
{
} public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}

上面我们给Test类添加一个静态的构造函数。

图4 Test类的IL代码

通过上面Test类的IL代码的区别我们发现,当Test类包含静态字段,而且没有定义静态的构造函数时,该类会被标记为beforefieldinit。

现在也许有人会问:“被标记为beforefieldinit和没有标记的有什么区别呢”?OK现在让我们通过下面的具体例子看一下它们的区别吧!

class Test
{
public static string x = EchoAndReturn("In type initializer"); static Test()
{
} public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
} class Driver
{
public static void Main()
{
Console.WriteLine("Starting Main");
// Invoke a static method on Test
Test.EchoAndReturn("Echo!");
Console.WriteLine("After echo");
Console.ReadLine(); // The output result:
// Starting Main
// In type initializer
// Echo!
// After echo
}
}

我相信大家都可以得到答案,如果在调用EchoAndReturn()方法之前,需要完成静态成员的初始化,所以最终的输出结果如下:

图5输出结果

接着我们在Main()方法中添加string y = Test.x,如下:

public static void Main()
{
Console.WriteLine("Starting Main");
// Invoke a static method on Test
Test.EchoAndReturn("Echo!");
Console.WriteLine("After echo"); //Reference a static field in Test
string y = Test.x;
//Use the value just to avoid compiler cleverness
if (y != null)
{
Console.WriteLine("After field access");
}
Console.ReadKey(); // The output result:
// In type initializer
// Starting Main
// Echo!
// After echo
// After field access }

图6 输出结果

通过上面的输出结果,大家可以发现静态字段的初始化跑到了静态方法调用之前,Wo难以想象啊!

最后我们在Test类中添加一个静态构造函数如下:

class Test
{
public static string x = EchoAndReturn("In type initializer"); static Test()
{
} public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}

图7 输出结果

理论上,type initializer应该发生在”Starting Main”之后和”Echo!”之前”,但这里却出现了不唯一的结果,只有当Test类包含静态构造函数时,才能确保type initializer的初始化发生在”Starting Main”之后和”Echo!”之前”。

所以说要确保type initializer发生在被字段引用时,我们应该给该类添加静态构造函数。

beforefieldinit释义的更多相关文章

  1. beforefieldinit释义(3)

    1.看下面的例子: public static class MyClass<T> { public static readonly DateTime Time = GetNow(); pr ...

  2. beforefieldinit释义(2)

    首先来看一段代码: using System; namespace BeforeFieldInit { internal class Foo { Foo(){ Console.WriteLine(&q ...

  3. 常见linux命令释义(第八天)—— Bash Shell 的操作环境

    换了新公司,公司的领导很不错.自己感受比较多的地方是,自己的工作效率明显比以前高了.以前会对频繁变动的需求十分不耐烦,现在接到需求后会仔细的思考,进行整体构建.即使以后需求有变动,也能够比较轻易的在原 ...

  4. 常见linux命令释义(第三天)

    今天晚上看鸟哥的私房菜,边学边写笔记. 在linux中压缩大多是.tar, .tar.gz , .tgz, /gz, .bz2等. .gz 是通过gzip压缩的文件. .bz2 是通过bzip2压缩的 ...

  5. 常见linux命令释义(第一天)

    快到中午吃饭了,然后忽然想起来samba里面没有添加用户.于是乎,就玩弄起了samba. Samba三下五除二就安装好了,想想window里面不断的点击下一步,还要小心提防各种隐藏再角落里的绑定软件. ...

  6. Tomcat的目录结构、处理流程、主配置文件(server.xml)释义

    参考资料: http://www.cnblogs.com/xdp-gacl/p/3744053.html http://grass51.blog.51cto.com/4356355/1123400 1 ...

  7. 使用Perl5获取有道词典释义

    Get Word Definition from dict.youda.com via Perl Script 获取基本释义 Get Basic Definition http://dict.youd ...

  8. Web前端名词释义及原理

    引言:看题目的时候,不要觉得这是一个很深奥的问题,Web前端这些东西很多就是叫的名字牛逼,其实原理很TM简单,也就那么回事. 一.javascript名词释义 1.啥是事件队列? 就是 弄一个数组,里 ...

  9. linux内核的冒险md来源释义# 14raid5非条块读

    linux内核的冒险md来源释义# 14raid5非条块读 转载请注明出处:http://blog.csdn.net/liumangxiong 假设是非条块内读.那么就至少涉及到两个条块的读,这就须要 ...

随机推荐

  1. AC自动机跟随Kuangbing学习笔记

    http://www.cnblogs.com/kuangbin/p/3164106.html kuangbin的博客 第一段代码基本是COPY kuangbin的.. 1.HDU 2222 Keywo ...

  2. Node.js实践第一天

    实践案例一 表单提交 demo.html <!doctype html> <html> <head> <title>表单提交</title> ...

  3. 认证和注册,提交到App Store:

    账号分为个人和公司个人付费后2天左右,收到激活邮件,可以使用 member center certificates,identifiers&profiles:certificates 认证:d ...

  4. hdu2317Nasty Hacks

    Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of mali ...

  5. hbase 二级索引创建

    在单机上运行hbase 二级索引: import java.io.IOException; import java.util.HashMap; import java.util.Map; import ...

  6. linux的find指令详解。

    引用:http://os.51cto.com/art/200908/141411.htm http://www.oschina.net/translate/15-practical-linux-fin ...

  7. python 2.6升级到2.7

    CentOS 6.5上安装的python版本是2.6.6,不能满足我运行软件的要求,所以对python进行升级. 原以为这也就是安装个软件的事儿,在我求稳搜索一下了之后发现,也并不是那么单纯简单. 下 ...

  8. ng-if和ng-show的区别

    angularJS中的ng-show.ng-hide.ng-if指令都可以用来控制dom元素的显示或隐藏.ng-show和ng-hide根据所给表达式的值来显示或隐藏HTML元素.当赋值给ng-sho ...

  9. docker 使用Data Volume 共享文件

    Adding a data volume You can add a data volume to a container using the -v flag with the docker run  ...

  10. 加密芯片ALPU

    加密芯片ALPU 纽文微电子(上海)有限公司 n  公司简介 NEOWINE是一家半导体开发公司,2002年6月成立于韩国,于2011年在中国设法人;  研发总部位于韩国京畿道,并在上海.深圳设办事处 ...