【OOP】迭代、泛型、委派的綜合範例

/
1 Comments

前言:

記錄一個用到迭代、泛型及委派的好用範例,也就是我們用Linq常用到的"where方法"!



程式碼如下:

public static class Utilities
{
 public static IEnumerable<T> ToWhere<T>(this IEnumerable<T> source
      , Func<T, bool> predicate)
 {
  foreach (T item in source)
  {
   if (predicate.Invoke(item))
   {
    yield return item;
   }
  }
 }
}


實際使用 :
void Main()
{
 List<string> list = new List<string>();
 list.Add("Tom");
 list.Add("Alex");
 list.Add("Ace");

 var a = list.ToWhere(x => x.Contains("A"));
 a.Dump();
}

輸出結果:


You may also like

1 則留言:

  1. Baccarat | All the Baccarat in the US - Wolverione
    The Best Baccarat Casinos · 1. Caesars Casino, 바카라 Las Vegas 제왕 카지노 – Best Online Casino · 2. Las Vegas Golden Nugget Casino, Las Vegas – Best for Beginners · 3. WynnBET 1xbet Casino

    回覆刪除