Why did the BeforeFieldInit behavior change in .NET 4?

一世执手 提交于 2019-12-23 11:24:09

问题


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.

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?


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/6729841/why-did-the-beforefieldinit-behavior-change-in-net-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!