Is there any overhead in the use of anonymous methods?

There is a small difference in how named methods and anonumous methods are handled when you create a delegate from them.

Delegates for anonymous methods are cached, so there is a small overhead for checking if the delegate already exists in the cache. On the other hand, if you run the method more than once, it will reuse the cached delegate instead of creating a new one.

Delegates for named methods are not cached, so it will be created each time.

Other than that there is no difference. The anonumous method will be created at compile time and exists in the code just like a regular method, only with a name that only the compiler knows about.


First, you probably shouldn't put a lot of code into an anonymous method. It would be more readable if you create a separate method for that, or even better, several methods.

As for the IL generated, if the lambda doesn't close over any variables, then the generated IL code is the same as if you put the code in normal named method (except that the generated method has an unspeakable name).

On the other hand, if you do close over some variable, the compiler creates a closure class to hold that variable in a field. And field access is slightly more expensive that local variable access.

To sum up, if you close over some variables, there is small overhead (including more objects that need to be garbage collected). In most situations, this doesn't matter and worrying about this would be premature optimization. But if you think it does matter, you should profile the code.