Understanding lambda parameters of a delegate in a function

3

I have the function below. I just want to understand who X is and what values it is reaching in the course of the function. I could not get the debug. They do not have to say exact, between values literally, but like it being aringido. I'm studying and I want to understand all of this.

public int qualquerCoisa(int tamanho)
        {
            Func<int, int> calcFib = null;

            calcFib = x => (x - 1 + x - 2);

            return calcFib(tamanho);
        }
    
asked by anonymous 09.07.2015 / 12:12

2 answers

2

The X represents the parameter that is passed to the calcFib function. If you rewrite the calcFib function as a method it would look like:

public int calcFic(int x)
{
    return x - 1 + x -2;
}

In the case of the code that you put, X will get the value of tamanho .

    
09.07.2015 / 12:26
2

Func<int,int> declares a delegate that represents a function that receives a parameter of type int and returns a value of type int .

Taking your example the result of return calcFib(tamanho); will be:

int resultado = tamanho - 1 + tamanho - 2;  

The value that x receives is the value that is passed to the function.

    
09.07.2015 / 12:26