Question

My understanding of the JIT compiler is very basic. From what I understand, the first time the JIT compiler comes across a piece of MSIL Code, it is compiled into native code and held in memory for later use (so the JIT compiler doesn't have to compile the same MSIL code again).

Is it the same idea for event handlers? When an event occurs in an application, does the JIT Compiler compile the code in the event handler for that event at runtime? If so, is the compiled native code cached?

Was it helpful?

Solution

Is it the same idea for event handlers?

An event handler is just a delegate, which in turn, will always refer to a method. Since the JIT will compile a method and cache it, the same thing happens for all event handlers, since they're underlying code is just a method.

If you're referring to event handlers defined and written using a lambda expression, ie:

SomeEvent += (o,e) => DoFoo();

Internally, this is still a normal method. The compiler converts the lambda into a normal method in the compiled IL. The same is true for anonymous methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top