C# Linq SkipLast Method

The SkipLast() method in C# LINQ is used to skip a specified number of elements from the end of a sequence and return the remaining elements. This method is particularly useful when you need to exclude the last few items from a collection.

Syntax

Following is the syntax for the SkipLast() method −

public static IEnumerable<TSource> SkipLast<TSource>(
    this IEnumerable<TSource> source, 
    int count
)

Parameters

  • source − The sequence to skip elements from.

  • count − The number of elements to skip from the end of the sequence.

Return Value

Returns an IEnumerable<T> that contains the elements from the input sequence minus the specified number of elements from the end.

SkipLast(2) Operation 95 90 88 85 50 45 Returned Elements Skipped (Last 2)

Using SkipLast() with Ordered Data

The following example demonstrates skipping the last two elements after sorting the array in descending order −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        int[] marks = { 45, 88, 50, 90, 95, 85 };
        
        IEnumerable<int> selMarks = marks.OrderByDescending(s => s).SkipLast(2);
        
        Console.WriteLine("Original array:");
        Console.WriteLine(string.Join(", ", marks));
        Console.WriteLine("\nAfter sorting descending and skipping last 2:");
        
        foreach (int res in selMarks) {
            Console.WriteLine(res);
        }
    }
}

The output of the above code is −

Original array:
45, 88, 50, 90, 95, 85

After sorting descending and skipping last 2:
95
90
88
85

Using SkipLast() with Original Order

Here's an example that skips elements from the original sequence without sorting −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };
        
        IEnumerable<string> result = names.SkipLast(2);
        
        Console.WriteLine("Original names:");
        Console.WriteLine(string.Join(", ", names));
        Console.WriteLine("\nAfter skipping last 2 names:");
        
        foreach (string name in result) {
            Console.WriteLine(name);
        }
    }
}

The output of the above code is −

Original names:
Alice, Bob, Charlie, David, Eve

After skipping last 2 names:
Alice
Bob
Charlie

Edge Cases

The SkipLast() method handles edge cases gracefully −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        int[] numbers = { 1, 2, 3 };
        
        // Skip more elements than available
        var result1 = numbers.SkipLast(5);
        Console.WriteLine("Skip 5 from 3 elements: " + result1.Count());
        
        // Skip zero elements
        var result2 = numbers.SkipLast(0);
        Console.WriteLine("Skip 0 elements: " + string.Join(", ", result2));
        
        // Empty collection
        int[] empty = { };
        var result3 = empty.SkipLast(2);
        Console.WriteLine("Skip from empty collection: " + result3.Count());
    }
}

The output of the above code is −

Skip 5 from 3 elements: 0
Skip 0 elements: 1, 2, 3
Skip from empty collection: 0

Common Use Cases

  • Removing trailing elements from data processing pipelines

  • Excluding footer records from file processing

  • Getting all items except the most recent ones in time-ordered data

  • Pagination scenarios where you need to exclude the last few items

Conclusion

The SkipLast() method provides an efficient way to exclude a specified number of elements from the end of any sequence. It works seamlessly with LINQ queries and handles edge cases like empty collections or skip counts larger than the sequence size by returning an empty result.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements