2014-09-01 79 views
-1

好吧,所以我試圖將ForEach擴展方法添加到linq,因爲沒有forEach for IEnumerables但由於某種原因,我看不到它。無法將擴展方法添加到LINQ

擴展方法是:

public static IEnumerable<TSource> ForEach<TSource>(Func<TSource> action) 
{ 
    yield return action(); 
} 

,當我嘗試調用它(gdMainGrid):

+2

這不是一個擴展方法。 – Dirk 2014-09-01 10:54:54

+2

首先,你不覺得一個* extension *方法需要一個'this'第一個參數嗎?爲什麼不在MSDN上查找任何擴展方法來查看它們的簽名是什麼?最後...... Enumerable.ForEach。嘆。 – Jon 2014-09-01 10:55:09

+0

是的,我忘了這一點,但即使添加了這個,它不起作用.. http://i.imgur.com/M94OpQJ.png – 2014-09-01 11:00:58

回答

1

要具有智能感知顯示的東西,你在你的代碼嘗試,你必須添加一個參數。 this IEnumerable<TSource> source

或者它不會是一個IEnumerable<TSource>一個擴展方法(和gdMain.Children.Cast<UIElement>()會返回一個IEnumerable<UIElement>

public static IEnumerable<TSource> ForEach<TSource>(this IEnumerable<TSource> source, Func<TSource> action) 
{ 
    yield return action(); 
}