- using System;
- using System.Collections.Generic;
- using System.Text;
- public static class myEnumerable
- {
- public static IEnumerable<T> Where<T>(
- this IEnumerable<T> source, Func<T, bool> predicate)
- {
- foreach (T item in source)
- if (predicate(item))
- yield return item;
- }
- }
- class ExtensionMethods
- {
- public static void Main()
- {
- string[] names = { "jak", "中国人", "Ivan","Harris", "Dad" };
- var mystr = names.Where(s => s.Length == 3);
- foreach(var str in mystr)
- {
- Console.Write("姓名:{0}/n",str);
- }
- Console.ReadKey();
- }
- }
扩展方法Where的实现示意
最新推荐文章于 2021-02-18 21:16:48 发布