Func为参数化匿名函数提供了一个持有人。前导类型是输入,最后一个类型总是返回值。
// 平方一个数字。
Func<double, double> square = (x) => { return x * x; };
// 得到平方根。
// 请注意签名如何与内置方法匹配。
Func<double, double> squareroot = Math.Sqrt;
// 提供您的工作。
Func<double, double, string> workings = (x, y) =>
string.Format("The square of {0} is {1}.", x, square(y))动作对象就像void方法,因此它们只有输入类型。没有结果放在评估堆栈上。
// 直角三角形。
class Triangle
{
public double a;
public double b;
public double h;
}
// 勾股定理。
Action<Triangle> pythagoras = (x) =>
x.h= squareroot(square(x.a) + square(x.b));
Triangle t = new Triangle { a = 3, b = 4 };
pythagoras(t);
Console.WriteLine(t.h); // 5,