Local Functions, Coroutines, Delegates i.e. why I was wrong

Originally I wrote a now removed post on Local Functions and coroutines and told you they were better than lambdas.

I was wrong.

Local Functions are still great if you want to improve performance, especially in this example:

The silly assumption I made was that they would be faster when you pass them around. In reality, I should have profiled this for myself as it was a different use case compared to the above.

You don’t save any memory by passing local functions around.

We went back to our favourite website to find out why:


If you have read that article, you may have worked out why passing a local function into a wait while or equivalent is not saving you anything.

Whenever you pass a function as an action instead of just passing a reference it actually makes a new Action with your function inside it and allocates memory. It essentially doesn’t save you any memory compared to creating using a lambda.

There is a way around it using static Actions that Jackson discusses in his article and is worth considering if you want to eek out performance.