标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 委托 { /// <summary> /// 扩展方法三要素 静态类、静态方法、this关键字 /// </summary> public static class ListExt { public static List<int> MyFindAll(this List<int> listInput, Predicate<int> compareFunction) { List<int> listResult = new List<int>(); foreach (int i in listInput) { if(compareFunction(i)) { listResult.Add(i); } } return listResult; } public static List<T> MyFindAll<T>(this List<T> listInput, Predicate<T> compareFunction) { List<T> listResult = new List<T>(); foreach (T t in listInput) { if (compareFunction(t)) { listResult.Add(t); } } return listResult; } } }
private static void Test2() { List<int> list = new List<int>{ 1,2,3,4 }; //List<int> listRestult = list.FindAll(IsOk); //List<int> listRestult = list.FindAll(a=>a>3); //List<int> listRestult = list.MyFindAll(IsOk); //List<int> listRestult = list.MyFindAll(a => a > 3); //foreach (int i in listRestult) //{ // Console.WriteLine(i.ToString()); //} //List<int> listRestult = list.MyFindAll<int>(IsOk); List<int> listRestult = list.MyFindAll<int>(a => a > 3); foreach (int i in listRestult) { Console.WriteLine(i.ToString()); } }
标签:
原文地址:http://www.cnblogs.com/caohuimingfa/p/4980073.html