Lamda表达式
关于Lamda表达式,我个人认为如果说,上篇所写的那些C#3.0的新特性并不是特别的吸引人的话,那么Lambda表达式的确能给我带来愉悦的感受,并且缩短了代码量。
作为C#3.0版本的委托对象,从代码上很容易就能够理解,比以前的委托要方便,简洁很多很多
格式:参数列表=>表达式/语句块
(x,y)=>x*y //多参数
x=x*19 //单参数 隐式类型/表达式
x=>{return x*10} //单参数 隐式类型/语句块
(int x)=>x*10 //单参数 显示类型=>表达式
(int x)=>{return x*10} //单参数 显示类型语句块
()=>Console.WriteLine() //无参数的表达式
以下给出自己测试Demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DemosTest;
namespace DemosTest
{
static class Program
{
//扩展方法
static void Foo(this string a)
{
Console.WriteLine(string.Format("this is {0}", a));
}
static void Lambda()
{
List<string> list = new List<string> {"abc","bcd","cde","def"};
![](/Images/OutliningIndicators/InBlock.gif)
//list.FindAll(delegate(string a) { return a.Contains("b"); }); //delegate 相比以下的Lamda,看起来就不那么爽了
var list_1=list.FindAll(s => s.Contains("b")); //Lambda表达式,自动识别S为string
foreach (var i in list_1)
i.Foo();
![](/Images/OutliningIndicators/InBlock.gif)
list_1 = list.FindAll(s => write(s)); //Lambda表达式,语句块的Demo
foreach (var i in list_1)
i.Foo();
}
![](/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
{
Lambda();
Console.ReadKey();
}
}
}
感觉用了C#3.0的Lamda后,再叫我用delegate,感觉就不爽了,我个人认为C#3.0的特性就是人性化,感觉到了它的自动识别类型的确很棒,很适合我这种懒人,Lamda就这点内容吧,继续学习......
关于Lamda表达式,我个人认为如果说,上篇所写的那些C#3.0的新特性并不是特别的吸引人的话,那么Lambda表达式的确能给我带来愉悦的感受,并且缩短了代码量。
作为C#3.0版本的委托对象,从代码上很容易就能够理解,比以前的委托要方便,简洁很多很多
格式:参数列表=>表达式/语句块
(x,y)=>x*y //多参数
x=x*19 //单参数 隐式类型/表达式
x=>{return x*10} //单参数 隐式类型/语句块
(int x)=>x*10 //单参数 显示类型=>表达式
(int x)=>{return x*10} //单参数 显示类型语句块
()=>Console.WriteLine() //无参数的表达式
以下给出自己测试Demo
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)