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 妈妈生日祝福语简短励志