Consider a local function such as Calc
in
void Foo()
{
var a = 500f;
var seed = 0.5f;
var b = 2000 * Calc(a);
.... more code ....
seed = 0.7f;
if (Calc(5000) > 5)
{
}
double Calc(float f)
{
return Sin(f+ seed) * f;
}
}
The local function is used normally without converting it into a delegate. How does it compare to inlining the code in both places:
- Is it semantically different from inlining?
- How are variables such as
seed
passed to the local function? I am hoping that the compiler just rewrites the function to accept additional parameters from the call site (ref float seed
in this case.) But from what I’ve read I think it’s more complicated, and possibly uses captures. But there’s no clear documentation about this. - Are hidden classes ever generated?
- Does silent boxing ever occur? I would hope not, but C# is a boxy language.
- In short, are local functions expected to be slower than manually inlining the code? Is a separate call frame always set up?
I found this question: Local function vs Lambda C# 7.0 but it doesn't answer this question in all its parts.
Note that this question is about local functions in general, and not just about this particular example.