public void Execute() { // // Skip拡張メソッドは、シーケンスの先頭から指定された件数分をスキップするメソッド。 // // ・シーケンスの要素数より多い数を指定した場合、空のシーケンスが返る. // ・0以下の値を指定した場合、シーケンスの全ての要素が返る. // var names = new[] {"gsf_zero1", "gsf_zero2", "gsf_zero3", "gsf_zero4", "gsf_zero5"}; Output.WriteLine("================ Skip ==========================="); var last2Elements = names.Skip(3); foreach (var item in last2Elements) { Output.WriteLine(item); } Output.WriteLine("シーケンスの要素数以上の数を指定: COUNT={0}", names.Skip(20).Count()); foreach (var item in names.Skip(-1)) { Output.WriteLine(item); } // // SkipWhile拡張メソッドは、指定された条件が満たされる間シーケンスから要素を抽出し // 返すメソッド。 // Output.WriteLine("================ SkipWhile ======================"); var greaterThan4 = names.SkipWhile(name => int.Parse(name.Last().ToString()) <= 3); foreach (var item in greaterThan4) { 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"); }
public void SlicingArrays() { var array = new[] { "peanut", "butter", "and", "jelly" }; Assert.AreEqual(new string[] { FILL_IN_THE_STRING, FILL_IN_THE_STRING}, array.Take(2).ToArray()); Assert.AreEqual(new string[] { FILL_IN_THE_STRING, FILL_IN_THE_STRING}, array.Skip(1).Take(2).ToArray()); }