C#表达式Lambda

Cal*_*Cal 2 c# lambda visual-studio-2010

嗨,我正在学习如何从一本书中使用Lambda.在我将书中的一段代码复制到VS2010后,我得到了错误:

代表' System.Func<float>'不接受1个论点"

VS2010在"浮动x"之前的第3行左括号下标记了错误.你能告诉我出了什么问题吗?

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();
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 9

你试图写一个函数接受一个float输入,并返回一个float输出.那是一个Func<float, float>.(为了给出一个更清晰的例子,如果你想要一个带int参数和返回类型的委托float,那将是一个Func<int, float>.)

A Func<float>没有参数,返回类型为float.来自以下文件Func<TResult>:

封装没有参数的方法,并返回参数指定的类型的值TResult.

public delegate TResult Func<out TResult>()
Run Code Online (Sandbox Code Playgroud)

  • @Garry你投票给答案,而不是书. (2认同)