问题描述
class MyClass
{
public string MyProperty { get; set; }
public void MyMethod()
{
//Do something difficult here
//100500 lines of code here ...
}
}
我们有很多 MyClass
的实例.
CLR是否为该类的任何实例创建此真正消耗内存的 MyMethod()
?
Does CLR creates this really memory-expensive MyMethod()
for any instance of the class?
推荐答案
不,不是.当我们第一次调用此方法时,此方法将编译一次.然后,编译后的代码将被任何 MyClass
类型的实例使用.因此,任何性能下降都只会在该方法的第一次调用中发生,它将从IL代码编译为本机代码.
No it does not. This method will compiled once, when we have the first call of this method. Then the compiled code will be used by any instance of type MyClass
. So any performance hit happens only in the first call of this method, which from IL code will be compiled to native code.
下面,我贴了两张图片,这些图片可能会更清楚:
Below, I posted two images that may make this more clear:
和
有关更多信息,请查看这本书通过C#进行CLR .
For further information, please take a look at this book CLR via C#.
这篇关于CLR在哪里为一种类型的实例存储方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!