我有一個關於使用lambda表達式的問題。Linq-Extensions,代碼不會返回偶數值c#
在教科書中有一個短賦值,其中給出了一系列整數0,2,4,8,7,10,3,2
,從第三項開始,只有整數值將被返回,使用Skip()
和TakeWhile()
。我想我說得對,但只有4,8
被退回,所以我偷偷摸摸地看了解解決方案,而我的解決方案實際上是一樣的。
你們能幫我嗎?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SkipTake
{
class Program
{
static void Main(string[] args)
{
int[] sequence= new int[] { 0, 2, 4, 8, 7, 10 , 3, 2 };
var select = sequence.Skip(2).TakeWhile(n => n % 2 ==0);
foreach (var item in select)
Console.WriteLine(item);
}
}
}
你是對的!一旦Predicate返回「false」,就是這樣。我應該更精確地閱讀>。< –