Search code examples
c#lambdainlining

Do lambdas get inlined?


Do simple lambda expressions get inlined?

I have a tendency (thanks to F# and other functional forays) to encapsulate repeated code present within a single function into a lambda, and call it instead. I'm curious if I'm incurring a run-time overhead as a result:

var foo = a + b;
var bar = a + b;

vs

Func<T1, T2> op = () => a + b;
var foo = op();
var bar = op();

Which one costs more to run?


Solution

  • No. Lambda functions are not inlined but instead are stored as delegates under the hood and incur the same cost of execution as other delegates.