Taking the code and putting it through sharplab.io , we can see that the code drops to:
public class Class { [CompilerGenerated] private sealed class <>c__DisplayClass0_0 { public string holder; internal void <Caller>b__0(string s) { this.holder = s; } internal void <Caller>g__SetHolder1(string s) { this.holder = s; } } public void Caller() { Class.<>c__DisplayClass0_0 @object = new Class.<>c__DisplayClass0_0(); Action<string> setHolder = new Action<string>(@object.<Caller>b__0); this.DoStuff(setHolder); this.DoStuff(new Action<string>(@object.<Caller>g__SetHolder1)); } public void DoStuff(Action<string> setHolder) { setHolder("holders new string"); } }
Since both act and SetHolder are closures over holder , when Caller is called, a new instance of the closure class is created, and new Action delegates are created for the lambda and local function. Thus, the resulting code is identical for both.
Therefore, given how you use them here, it just comes down to readability (as many R # recommendations do). Local functions may have better syntax, so R # recommends using it that way.
source share