int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
这些是来自http://msdn.microsoft.com/en-us/library/bb397687.aspx
的C#代码我理解前两个lambda表达式就好了,将n视为数组的一个元素&#34;数字&#34;。
然而,第三个lambda表达式确实与&#34; index&#34;混淆。是(n,index)为数组建立的lambda参数之一吗?这是一个惯例吗?
答案 0 :(得分:1)
当TakeWhile
遍历集合时:
n
是元素
的值
index
是元素
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// As TakeWhile iterates over the array:
// "n" is the value of the element
// "index" is the index of the element
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
foreach(var n in firstSmallNumbers)
Console.WriteLine(n);
输出:
5
4
在以下位置运行:https://dotnetfiddle.net/4NXRkg