C# 程序从末尾跳过数组元素

csharpprogrammingserver side programming更新于 2025/9/15 17:41:00

声明一个数组并初始化元素。

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("跳过最后 3 个元素后的数组...");

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

输出

数组...
45
50
60
70
85
跳过最后 3 个元素后的数组...
45
50

相关文章