public void Execute() { // // Take拡張メソッドは、シーケンスの先頭から指定された件数分を返すメソッド。 // // ・シーケンスの要素数より多い数を指定した場合、そのシーケンス全てが返る. // ・0以下の値を指定した場合、空のシーケンスが返る. // var names = new[] {"gsf_zero1", "gsf_zero2", "gsf_zero3", "gsf_zero4", "gsf_zero5"}; Output.WriteLine("================ Take ======================"); var top3 = names.Take(3); foreach (var item in top3) { Output.WriteLine(item); } foreach (var item in names.Take(20)) { Output.WriteLine(item); } Output.WriteLine("0以下の数値を指定: COUNT={0}", names.Take(-1).Count()); // // TakeWhile拡張メソッドは、指定された条件が満たされる間シーケンスから要素を抽出し // 返すメソッド。 // Output.WriteLine("================ TakeWhile ======================"); var lessThan4 = names.TakeWhile(name => int.Parse(name.Last().ToString()) <= 4); foreach (var item in lessThan4) { Output.WriteLine(item); } }
public static void Test(Assert assert) { assert.Expect(8); // TEST var numbers = new[] { 1, 3, 5, 7, 9 }; var firstTwo = numbers.Take(2).ToArray(); assert.DeepEqual(firstTwo, new[] { 1, 3 }, "Take() the first two array elements"); // TEST var lastThree = numbers.TakeFromLast(3).ToArray(); assert.DeepEqual(lastThree, new[] { 5, 7, 9 }, "TakeFromLast() the last three array elements"); // TEST var exceptTwoLast = numbers.TakeExceptLast(2).ToArray(); assert.DeepEqual(exceptTwoLast, new[] { 1, 3, 5 }, "TakeExceptLast() the first array elements except the last two"); // TEST var takeWhileLessTwo = numbers.TakeWhile((number) => number < 2).ToArray(); assert.DeepEqual(takeWhileLessTwo, new[] { 1 }, "TakeWhile() less two"); // TEST var takeWhileSome = numbers.TakeWhile((number, index) => number - index <= 4).ToArray(); assert.DeepEqual(takeWhileSome, new[] { 1, 3, 5, 7 }, "TakeWhile() by value and index"); // TEST var skipThree = numbers.Skip(3).ToArray(); assert.DeepEqual(skipThree, new[] { 7, 9 }, "Skip() the first three"); // TEST var skipWhileLessNine = numbers.SkipWhile(number => number < 9).ToArray(); assert.DeepEqual(skipWhileLessNine, new[] { 9 }, "SkipWhile() less then 9"); // TEST var skipWhileSome = numbers.SkipWhile((number, index) => number <= 3 && index < 2).ToArray(); assert.DeepEqual(skipWhileSome, new[] { 5, 7, 9 }, "SkipWhile() by value and index"); }