C# 程式跳過陣列中指定數量的元素


以下是我們的陣列 -

int[] points = { 210, 250, 300, 350, 420};

使用 skip() 方法跳過元素。新增一個數字作為引數,表示要返回的元素數量。

IEnumerable<int> skipEle = points.AsQueryable().OrderByDescending(s => s).Skip(3);

示例

 線上演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] points = { 210, 250, 300, 350, 420};
      Console.WriteLine("Initial array...");
   
      foreach (int res in points)
      Console.WriteLine(res);
      IEnumerable<int> skipEle = points.AsQueryable().OrderByDescending(s => s).Skip(3);
      Console.WriteLine("Skipped 3 elements...");

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

輸出

Initial array...
210
250
300
350
420
Skipped 3 elements...
250
210

更新於: 2020 年 6 月 23 日

521 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.