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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP