Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# ข้ามองค์ประกอบแรกในอาร์เรย์

ใช้เมธอด Skip(n) ข้าม n องค์ประกอบแรกของลำดับ

ตัวอย่าง: ข้าม 4 ตัวแรก

using System;
using System.Linq;

public class Demo
{
    public static void Main()
    {
        int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 };
        Console.WriteLine("Initial Array...");
        foreach (var res in arr) Console.WriteLine(res);
        
        var ele = arr.Skip(4); // ข้าม 4 ตัวแรก
        
        Console.WriteLine("New Array after skipping elements...");
        foreach (var res in ele) Console.WriteLine(res);
    }
}

ผลลัพธ์

Initial Array...
24
40
55
62
70
82
89
93
98
New Array after skipping elements...
70
82
89
93
98

ใช้ร่วมกับ Take: สำหรับ Pagination: source.Skip(pageSize * (page-1)).Take(pageSize)