为什么在.NET 4的BeforeFieldInit行为的改变? [英] Why did the BeforeFieldInit behavior change in .NET 4?

查看:98
本文介绍了为什么在.NET 4的BeforeFieldInit行为的改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#4,的类型,而 beforefieldinit 标记的行为改变了,所以现在一个类型初始化可以先使用类的任何静态字段之前调用。

In C# 4, the behavior of types without the beforefieldinit flag was changed, so now a type initializer can call before first use of any static field of the class.

我的问题是为什么在C#/。NET的团队改变了这种行为?主要是什么原因?你能告诉任何实际的例子,这种变化使任何意义?

My questions are why has the C#/.NET team changed that behavior? What is the main reason? Can you show any practical example where this change makes any sense?

推荐答案

的行为一直是一个什么样的边界内的文件的 - 它只是它被渴望慵懒在.NET 4中改变

The behaviour has always been within the bounds of what's documented - it's just that it changed from being eager to lazy in .NET 4.

我的犯罪嫌疑人的JIT的团队设法找到一种方法,使懒惰而没有性能损失......或者可能有助于性能别的地方。这很可能只是的一个的这种行为变化的.NET CLR 4在VS的.NET CLR 2 ...它发生,我注意到了这一点,但我怀疑,其他许多人一样。我认为这是完全合理的JIT团队调整的事情,因为他们认为合适的,所记录的保障范围内。

I suspect the JIT team managed to find a way to make it lazy without a performance penalty... or possibly it helps performance somewhere else. This is likely to only be one such change in behaviour within the .NET 4 CLR vs the .NET 2 CLR... it happens that I noticed it, but I doubt that many other people did. I think it's entirely reasonable for the JIT team to adjust things as they see fit, within the documented guarantees.

最后,如果这让你的code失败,您已经有了一个错误了。

Ultimately, if this makes your code fail, you've got a bug already.

这篇关于为什么在.NET 4的BeforeFieldInit行为的改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆