C# 程式從末尾跳過陣列元素


宣告一個數組並初始化元素。

int[] marks = { 45, 50, 60, 70, 85 };

使用 SkipLast() 方法跳過陣列元素,從末尾算起。

IEnumerable<int> selMarks = marks.AsQueryable().SkipLast(3);

元素被跳過,剩餘元素返回如下所示 -

示例

 線上演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] marks = { 45, 50, 60, 70, 85 };
      Console.WriteLine("Array...");

      foreach (int res in marks)
      Console.WriteLine(res);
      IEnumerable<int> selMarks = marks.AsQueryable().SkipLast(3);
      Console.WriteLine("Array after skipping last 3 elements...");

      foreach (int res in selMarks)
      Console.WriteLine(res);
   }
}

輸出

Array...
45
50
60
70
85
Array after skipping last 3 elements...
45
50

更新日期: 2020-06-23

1K+ 次瀏覽

開啟你的 職業

透過完成課程來獲得認證

立即開始
廣告
© . All rights reserved.