1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public static class EX { public static List<Tsource> Where<Tsource,T2>(this IEnumerable<Tsource> items,Func<Tsource, T2, bool> func, T2 temp) { List<Tsource> list = new List<Tsource>(); foreach (Tsource item in items) { if (func(item, temp)) { list.Add(item); } } return list; } }
class Program { static void Main(string[] args) { List<string> stringItems = new List<string>{ "张三","李四","王五","马六","王二麻子","钱掌柜","葫芦棍子","屎蛋" }; int limitLength = 2; Func<string, int, bool> func = (origin, temp) => origin.Length > temp; List<string> res = stringItems.MyWhere(func,limitLength); foreach (var item in res) { Console.WriteLine("名字长度大于2的项目:" + item); }
Console.Read(); } }
|