C# 程式可從陣列末尾返回特定數量的元素


使用 TakeLast() 方法從陣列末尾返回元素。

我們首先宣告並初始化一個數組。

int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};

現在,讓我們獲取最後三個元素。

IEnumerable<int> units = prod.AsQueryable().TakeLast(3);

讓我們看看完整的程式碼。

示例

 即時演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};
      // value of last three products
      IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
      foreach (int res in units) {
         Console.WriteLine(res);
      }
   }
}

輸出

698
765
789

更新於:2020 年 6 月 23 日

128 次瀏覽

開啟你的職業生涯

透過完成課程獲取證書

開始
廣告
© . All rights reserved.