C#功能与动作
示例
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,
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短