Hi I'm learning about using Lambda from a book. After I copied a piece of code from the book to VS2010 I got the error:
Delegate '
System.Func<float>
' does not take 1 arguments"
VS2010 marked the error under the left parenthesis at line 3, before "float x". Can you tell me what is wrong?
static void Main(string[] args)
{
Func<float> TheFunction = (float x) =>
{
const float A = -0.0003f;
const float B = -0.0024f;
const float C = 0.02f;
const float D = 0.09f;
const float E = -0.5f;
const float F = 0.3f;
const float G = 3f;
return (((((A * x + B) * x + C) * x + D) * x + E) * x + F) * x + G;
};
Console.Read();
}
You're trying to write a function which accepts a float
input, and returns a float
output. That's a Func<float, float>
. (To give a clearer example, if you wanted a delegate with an int
parameter and a return type of float
, that would be a Func<int, float>
.)
A Func<float>
would have no parameters, and a return type of float
. From the documentation of Func<TResult>
:
Encapsulates a method that has no parameters and returns a value of the type specified by the
TResult
parameter.public delegate TResult Func<out TResult>()
See more on this question at Stackoverflow