我在MSDN的Linq示例中找到了一个很不错的方法叫做Fold(),我想要使用它。他们的例子:
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
double product =
doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor);
很不幸,我无法使其编译通过,无论是在他们的示例中还是在我的自己代码中,并且我在MSDN中找不到任何其他地方提到这个方法(例如Enumerable或Array扩展方法)。我得到的错误是一个普通的“不知道那个”的错误:
error CS1061: 'System.Array' does not contain a definition for 'Fold' and no
extension method 'Fold' accepting a first argument of type 'System.Array' could
be found (are you missing a using directive or an assembly reference?)
我正在使用其他方法,我相信这些方法来自于Linq(如Select()和Where()),并且我已经"using System.Linq",所以我认为一切都没问题。
这个方法在C# 3.5中真的存在吗?如果存在,那么我做错了什么?