使用另一种方法作为参数

Wil*_*son 3 .net c# parameters methods

有没有办法将方法用作另一个方法的参数.例如,为给定函数f返回2f(3)的方法.据我所知,我的代码不正确:我试图传达我想要的想法.

static double twofof3(double f(double x))
{
    return 2*f(3);
}

static double f(double x)
{
   return x * x;
}
Run Code Online (Sandbox Code Playgroud)

twofof3方法目前毫无意义,因为它可以通过f方法实现,但它更多是我感兴趣的概念.

Blo*_*ard 7

是的,你可以使用Func代表:

static double twofof3(Func<double,double> f)
{
    return 2*f(3);
}

static double function1(double x)
{
   return x * x;
}

// ...

Console.WriteLine(twofof3(function1));
Run Code Online (Sandbox Code Playgroud)