如何在这里传递 Func
How to pass a Func here
假设我有一个 class 如下所示。
public class Circle
{
private double radius;
public double GetArea(Func<double, double> operand)
{
return operand(radius);
}
}
我不确定如何在运行时传递 Func 来进行所需的计算
var area = circle.GetArea((r) => 2 * Math.PI * r * r);
好像有点傻,何乐而不为
public class Circle
{
public Circle(double radius) { Radius = radius; }
public double Radius { get; private set; }
public double Area {
get {
return 2 * Math.PI * Radius * Radius;
}
}
}
我希望答案是不言自明的:
Func<double, double> multiply = (r)=>r*r;
double area = GetArea(multiply, radius);
public double GetArea( Func<double, double> operand , double rad )
{
return operand( rad );
}
假设我有一个 class 如下所示。
public class Circle
{
private double radius;
public double GetArea(Func<double, double> operand)
{
return operand(radius);
}
}
我不确定如何在运行时传递 Func 来进行所需的计算
var area = circle.GetArea((r) => 2 * Math.PI * r * r);
好像有点傻,何乐而不为
public class Circle
{
public Circle(double radius) { Radius = radius; }
public double Radius { get; private set; }
public double Area {
get {
return 2 * Math.PI * Radius * Radius;
}
}
}
我希望答案是不言自明的:
Func<double, double> multiply = (r)=>r*r;
double area = GetArea(multiply, radius);
public double GetArea( Func<double, double> operand , double rad )
{
return operand( rad );
}