Understanding lambda parameters of a delegate in a function

Posted on

Question :

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);


Answer :

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 .


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.


Leave a Reply

Your email address will not be published. Required fields are marked *