概述
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();
}
}
}
转载于:https://www.cnblogs.com/sunzgod/p/4171710.html
最后
以上就是愉快牛排为你收集整理的lambda表达式对比的全部内容,希望文章能够帮你解决lambda表达式对比所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复