2

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:

  1. Is it semantically different from inlining?
  2. 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.
  3. Are hidden classes ever generated?
  4. Does silent boxing ever occur? I would hope not, but C# is a boxy language.
  5. 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.

1
  • @HansPassant, I’ve extensively edited the question to make it clear that it isn’t a yes/no question, but rather requests detailed information. Commented Nov 16, 2019 at 6:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.