Search code examples
c#.netstaticc#-8.0local-functions

Why declare a local function static in C# 8.0


In C# 8.0, Static Local Functions are announced

Can anyone help enlighten me as to why you would want to declare a local function as static?

The reason given in in the article:

to ensure that local function doesn't capture (reference) any variables from the enclosing scope

But:

  1. I don't understand why would you want to ensure that?
  2. Is there any other reason or benefits to declare it static? (performance maybe?)

The example code given in the article is:

int M()
{
    int y = 5;
    int x = 7;
    return Add(x, y);

    static int Add(int left, int right) => left + right;
}

Solution

  • This answer from CodeCaster and this separate answer from György Kőszeg individually answer different parts of my question, so I'm bringing them both together to form the full picture for the accepted answer:

    For Part 1) of my question, @CodeCaster Says:

    Because it prevents you from shooting yourself in the foot. It forces the local function to be a pure function that does not modify the state of the caller.

    in larger codebases maintained by multiple people and not properly covered by unit tests, this simple modifier prevents grief

    So Answer 1 is: Static Local Functions ensure reliable caller method state.

    For Part 2) of my question, @György Kőszeg Says:

    Capturing variables has a small additional cost as it will generate an internally used type where your captured variables are public fields

    And he goes on to give an example of the produced compiler code via reflector.

    So Answer 2 is: Static Local Functions prevent variable capturing. Variable capturing comes at a small cost. So there is a small performance boost by declaring the local function static