0

I have never before seen the following crazy syntax, which can be used in the middle of a function:

void DoSomething()
{
    // https://learn.microsoft.com/en-us/visualstudio/ide/reference/convert-foreach-linq?view=vs-2019
    // How is this declaration possible?
    IEnumerable<int> crazy()
    {
        foreach (var num in Enumerable.Range(1, 5))
        {
            yield return num + 20;
        }
    }

    foreach (var thing in crazy())
    {
        Console.WriteLine(thing);
    }
}

I am very experienced with creating my own IEnumerable classes and using yield return. But I have never seen a combo declaration-implementation in the middle of a method like the above crazy() demonstrates. Is it some kind of lambda? If yes, why does it not follow lambda syntax? Is it a concrete type? What concrete type? Struct? Class? Is it literally a named function declaration in the middle of a method declaration? Since when was that possible?

I tried this with .Net Core 3.1, but apparently this syntax isn't very new.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234

0 Answers0