Will compiler optimize nested lambda that doesn't capture intermediate values? #79900
Answered
by
glen-nicol
glen-nicol
asked this question in
Q&A
-
Consider the following: IService service = ...;
CallFunc(x => CallFunc2(y => service.Process(y)); Since the inner lambda doesn't capture x, does the compiler optimize the creation of the y lambda such that its created once and then captured in the x lambda or is service captured and y recreated in each invocation of the x lambda? Does this technique or something like it have a name? |
Beta Was this translation helpful? Give feedback.
Answered by
glen-nicol
Aug 14, 2025
Replies: 1 comment 6 replies
-
That a look at the IL :-). It's the definitive answer here. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fascinating, seems the compiler does a cached lazy load of the nested lambda action?
Does this type of optimization where the closures are analyzed and simplified have a specific name?