我正在使用MSDN页面中的101个LINQ示例来学习LINQ,并且遇到了以下代码:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
foreach (var n in firstSmallNumbers)
{
Console.WriteLine(n);
}
此函数的目的是“使用TakeWhile从数组的开头开始返回元素,直到命中一个小于其在数组中位置的数字为止”。
究竟是怎么n
和index
知道哪些参数取?(即如何n
知道它将花费0、1、2、3 ...5, 4, 1, 3, 9, 8, 6, 7, 2, 0
以及如何index
知道)?
因为过载是通过这种方式定义的。从MSDN
public static IEnumerable<TSource> TakeWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate
)
该predicate
参数被描述如下:
测试条件中每个源元素的功能;函数的第二个参数表示源元素的索引。
该TSource
参数是项目,并且int
是该指数。该bool
是返回值。
编写时(n, index) => ...
,n
采用第一个参数(TSource
),index
采用第二个参数(int
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句