lambda表达式对比
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lambda
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("传统的委托代码示例:");
FindListDelegate();
Console.WriteLine("\n");
Console.WriteLine("使用匿名方法的示例:");
FindListAnonymousMethod();
Console.WriteLine("\n");
Console.WriteLine("使用Lambda的示例: ");
FindListLambdaExpression();
}
//传统调用委托的示例
private static void FindListDelegate()
{
List<string> list=new List<string>();
list.AddRange(new string[]{"ASP.NET课程","J2EE课程","PHP课程","数据结构课程"});
Predicate<string> finPredicate=new Predicate<string>(IsBookCategory);
List<string> BookCategory = list.FindAll(finPredicate);
foreach (string str in BookCategory)
{
Console.WriteLine("{0}\t",str);
}
}
private static bool IsBookCategory(string str)
{
return str.EndsWith("课程")?true:false;
}
private static void FindListAnonymousMethod()
{
List<string> list = new List<string>();
list.AddRange(new string[] { "ASP.NET课程", "J2EE课程", "PHP课程", "数据结构课程" });
List<string> bookCategory = list.FindAll(delegate(string str)
{
return str.EndsWith("课程")?true:false;
});
foreach (string str in bookCategory)
{
Console.WriteLine("{0}\t",str);
}
}
private static void FindListLambdaExpression()
{
List<string> list = new List<string>();
list.AddRange(new string[] { "ASP.NET课程", "J2EE课程", "PHP课程", "数据结构课程" });
List<string> bookCategory=list.FindAll((string str)=>str.EndsWith("课程"));
foreach (string str in bookCategory)
{
Console.WriteLine("{0}\t",str);
}
Console.ReadLine();
}
}
}
最新文章
- 虚拟化平台cloudstack(2)——安装(上)
- JS,Jquery获取各种屏幕的宽度和高度
- freeCodeCamp:Find the Longest Word in a String
- 【转】vs2012 打包安装更改 setup.exe的图标
- js的in运算符与instanceof运算符
- iOS与HTML5交互方法总结(转)
- VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)
- Away3D带你360&#176;漫游全景影像
- Laravel项目修改时区
- [js高手之路] es6系列教程 - 箭头函数详解
- Python中列表、元组、字典增删改查基本区别
- Javascript高级编程学习笔记(28)—— BOM(2)window对象2
- extern介绍
- 序列操作bzoj2962(未完成)
- 看图写代码---看图写代码 阅读<;<;Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >;>;
- 更好的转换规则(深入理解c#)
- 使用Font Awesome替换你的网站图标(icons 图标)
- socat 的神奇使用方式
- day5模块学习--shelve模块
- java截取字符串