Summary: in this tutorial, you will learn how to use the LINQ SkipLast() method to bypass a specified number of elements from the end of a sequence.
Introduction to the LINQ SkipLast() method
The LINQ SkipLast() method allows you to skip the last count elements from a sequence and return the remaining elements after skipping.
Here’s the syntax for using the SkipLast() method:
IEnumerable<T> SkipLast<TSource>(
this IEnumerable<TSource> source,
int count
)Code language: C# (cs)In this syntax:
sourceis the input sequence of elements.countis the number of elements to skip.
The SkipLast() method returns an IEnumerable<TSource> of the elements after skipping the last count elements.
LINQ SkipLast() method example
The following example uses the LINQ SkipLast() to skip the last three numbers in a list of integers:
using static System.Console;
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
var result = numbers.SkipLast(3);
foreach (var number in result)
{
WriteLine(number);
}Code language: C# (cs)Output:
1
2
3
4Code language: C# (cs)Summary
- Use the LINQ
SkipLast()method to bypass a specified number of elements from the end of a sequence and return the remaining elements after skipping.
Was this tutorial helpful ?