static uint Fibonacci(uint n)
{
return n <= 1 ? n : Fibonacci(n - 1) + Fibonacci(n - 2);
}
Func<uint> fibN = () => Fibonacci(n);
Func<int, int, int> add = (a, b) => a + b;
I understand add function syntax: it returns int result of a + b statement into which int a and b parameters “goes”.
But why fibN function has empty parameter block () ? Doesn’t n “goes” to this function as parameter? Please help me to grasp some understanding of this moment.
Consider the following code:
It does not comile, as soon as there is no variable
nof a suitable type to be used. However if you add:into
Mainmethod orto the class, the code will compile.
Let’s disassemble. For the following code:
we get:
In this code you may find a hidden class
c__DisplayClass1, in which we can see a public field of typeuint32namedn:and method
<Main>b__0:which actually calls
Fibonacciand passes thenvariable. So the compiler extracted the local variableninto a separate class; as well as extracted lambda into a method of this class. In the end it looks like you assignc__DisplayClass1.<Main>b__0toFunc<uint> fibN.