How is a local function in C # 7.0 different from a foreach or loop?

This is rather a question. Here it is.

C # 7.0 added a new function called "Local Function". Below is a snippet of code.

public int Fibonacci(int x)
{
    if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x));
    return Fib(x).current;

    (int current, int previous) Fib(int i)
    {
        if (i == 0) return (1, 0);
        var (p, pp) = Fib(i - 1);
        return (p + pp, p);
    }
}

What I do not understand is a recursive call of the same method. We can easily achieve this with the usual foreach. Then why is a local function.

MSDN says

implemented as iterators, a non-iterator wrapper method is usually required to accurately verify arguments during a call. (The iterator itself does not start until MoveNext is called).

Need help understanding the concept.

+4
source share
1 answer

. foreach

: (, ) . , .

- , , , .

, . .

+6

Source: https://habr.com/ru/post/1689368/


All Articles