Are static methods eagerly compiled (JIT'ed)?

前端 未结 2 2040
猫巷女王i
猫巷女王i 2021-02-08 08:43

Per my understanding, both instance methods and static methods are treated same by CLR compiler and the IL code is JITted whenever the method is called first time. Today I had a

2条回答
  •  面向向阳花
    2021-02-08 09:43

    As far as I know static methods are not threated differently from instance methods, maybe your colleague is talking about static constructors that actually are invoked as soon the type is referred in the calling assembly. and thus jitted.

    Update For 4.0 ( thanks @JulienLebosquain for pointing )

    .NET 4.0 uses a so called Lazy Type Initialization, that basically change the behavior and statics constructor are called just when as soon a static field is acceded the first time.

提交回复
热议问题