C#中Lambda表达式的用法
作者:springsnow 时间:2022-12-22 15:59:17
一、介绍
1、"Lambda表达式"是一个特殊的匿名函数,简化了匿名委托的使用,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量。
2、它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。
3、所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。Lambda表达式x => x * x读作"x goes to x times x"。
4、Lambda表达式语法:
无参数:()=>expr
一个参数:param=>expr
多个参数:(param-list)=>expr
注意点:如果方法体内只有一句代码,可以省略代码块符号,也就是不用写{ }
二、举例:
1、 委托实例方式
//委托 逛超市
delegate int GuangChaoshi(int a);
static void Main(string[] args)
{
GuangChaoshi gwl = JieZhang;
Console.WriteLine(gwl(10) + ""); //打印20,委托的应用
Console.ReadKey();
}
//结账
public static int JieZhang(int a)
{
return a + 10;
}
2、lambda表达式
//委托 逛超市
delegate int GuangChaoshi(int a);
static void Main(string[] args)
{
// GuangChaoshi gwl = JieZhang;
GuangChaoshi
gwl
=
p => p + 10
;
Console.WriteLine(
gwl(10)
+ ""); //打印20,表达式的应用
Console.ReadKey();
}
3、多参数
//委托 逛超市
delegate int GuangChaoshi(int a,int b);
static void Main(string[] args)
{
GuangChaoshi gwl = (p,z) => z-(p + 10);
Console.WriteLine(gwl(10,100) + ""); //打印80,z对应参数b,p对应参数a
Console.ReadKey();
}
三、内置委托
1、集合操作
public class Person
{
public string Name { get; set; }
public int Age { get;set; }
}
class Program
{
public static List<Person> PersonsList()
{
List<Person> persons = new List<Person>();
for (int i = 0; i < 7; i++)
{
Person p = new Person() { Name = i + "儿子", Age = 8 - i, };
persons.Add(p);
}
return persons;
}
static void Main(string[] args)
{
List<Person> persons = PersonsList();
persons = persons.Where(p => p.Age > 6).ToList(); //所有Age>6的Person的集合
Person per = persons.SingleOrDefault(p => p.Age == 1); //Age=1的单个people类
persons = persons.Where(p => p.Name.Contains("儿子")).ToList(); //所有Name包含儿子的Person的集合
}
}
2、Func<T>委托
static void Main(string[] args)
{
Func<int, int, bool> gwl = (p, j) =>
{
if (p + j == 10)
{
return true;
}
return false;
};
Console.WriteLine(gwl(5,5) + ""); //打印‘True',z对应参数b,p对应参数a
Console.ReadKey();
}
四、lambda表达式树动态创建方法
static void Main(string[] args)
{
//i*j+w*x
ParameterExpression a = Expression.Parameter(typeof(int),"i"); //创建一个表达式树中的参数,作为一个节点,这里是最下层的节点
ParameterExpression b = Expression.Parameter(typeof(int),"j");
BinaryExpression be = Expression.Multiply(a,b); //这里i*j,生成表达式树中的一个节点,比上面节点高一级
ParameterExpression c = Expression.Parameter(typeof(int), "w");
ParameterExpression d = Expression.Parameter(typeof(int), "x");
BinaryExpression be1 = Expression.Multiply(c, d);
BinaryExpression su = Expression.Add(be,be1); //运算两个中级节点,产生终结点
Expression<Func<int, int, int, int, int>> lambda = Expression.Lambda<Func<int, int, int, int, int>>(su,a,b,c,d);
Console.WriteLine(lambda + ""); //打印‘(i,j,w,x)=>((i*j)+(w*x))',z对应参数b,p对应参数a
Func<int, int, int, int, int> f= lambda.Compile(); //将表达式树描述的lambda表达式,编译为可执行代码,并生成该lambda表达式的委托;
Console.WriteLine(f(1, 1, 1, 1) + ""); //打印2
Console.ReadKey();
}
来源:https://www.cnblogs.com/springsnow/p/9441946.html
标签:C#,Lambda,表达式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java二维数组实现推箱子小游戏
2022-08-31 10:18:40
Android RippleDrawable 水波纹/涟漪效果的实现
2022-05-24 23:12:37
![](https://img.aspxhome.com/file/2023/3/138323_0s.gif)
Java多线程案例之单例模式懒汉+饿汉+枚举
2021-11-07 05:18:01
![](https://img.aspxhome.com/file/2023/0/77130_0s.png)
SpringCloud Eureka搭建的方法步骤
2021-10-10 11:03:29
![](https://img.aspxhome.com/file/2023/1/61241_0s.jpg)
如何基于LoadingCache实现Java本地缓存
2023-04-02 00:14:55
springboot整合vue实现上传下载文件
2023-11-14 07:10:37
![](https://img.aspxhome.com/file/2023/4/94374_0s.jpg)
SpringBoot配置文件方式,在线yml文件转properties
2023-02-25 08:43:54
![](https://img.aspxhome.com/file/2023/7/99917_0s.png)
解决mybatis一对多查询resultMap只返回了一条记录问题
2022-07-08 08:36:31
Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法
2023-11-19 00:24:05
Android中volley封装实践记录
2021-07-05 19:55:03
![](https://img.aspxhome.com/file/2023/2/139482_0s.jpg)
两种java实现二分查找的方式
2023-09-04 05:25:00
![](https://img.aspxhome.com/file/2023/3/98123_0s.jpg)
Android RollPagerView实现轮播图
2023-06-12 21:10:32
Android自定义带增长动画和点击弹窗提示效果的柱状图DEMO
2022-10-16 17:10:25
![](https://img.aspxhome.com/file/2023/2/129012_0s.gif)
Java实现简易Web服务器
2023-11-17 09:10:11
浅谈c#中config.exe 引发的一些问题
2022-10-20 07:11:07
Spring Boot接口限流的常用算法及特点
2023-12-20 05:16:30
![](https://img.aspxhome.com/file/2023/8/80308_0s.jpg)
unity 切换场景不销毁物体问题的解决
2022-04-29 11:26:06
rsa加密算法使用示例分享
2021-08-03 20:47:29
java中Locks的使用详解
2023-11-29 00:18:38
浅谈java实现背包算法(0-1背包问题)
2022-04-28 15:23:43