1 class Program
2 {
3 static void Main(string[] args)
4 {
5 List<string> list = new List<string>() {
6 "2","34","45","234"
7 };
8 List<string> lis = list.WhereExtFunc(m =>m.CompareTo("3")<0).ToList();
9
10
11 List<int> intList = new List<int>() {2,3,67,8 };
12 var intl = intList.WhereExtFunc(m => m < 5).ToList();
13 }
14 }
15 /// <summary>
16 /// 扩展方法
17 /// 在静态类中的静态方法
18 /// this 指向要扩展的类型
19 /// </summary>
20 public static class ListExt
21 {
22 //这里是一个List泛型扩展方法
23 //WhereString<T>不要忘了这里的<T>
24 public static List<T> WhereExtFunc<T>(this List<T> list, Func<T, bool> func)
25 {
26 List<T> newList = new List<T>();
27 foreach (var item in list)
28 {
29 if (func(item))
30 {
31 newList.Add(item);
32 }
33 }
34 return newList;
35 }
36 }