Lambda表达式
"Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量。它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。Lambda表达式x => x * x读作"x goes to x times x"。可以将此表达式分配给委托类型,如下所示:
- delegate int del(int i);
- del myDelegate = x => x * x;
- int j = myDelegate(5); //j = 25
简单例子
class Program { public delegate int Del(int i); public static Del del; static void Main(string[] args) { del = x => x + 3; Console.WriteLine(del(5)) ; } }
运行结果
8
请按任意键继续. . .
Lambda表达式Lambda表达式是由.NET 2.0演化而来的,也是LINQ的基础,熟练地掌握Lambda表达式能够快速地上手LINQ应用开发。