Skip<TSource> Method
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
Syntax
public static IEnumerable<TSource> Skip<TSource>(
this IEnumerable<TSource> source,
int count
)
Parameters
source
- Type:
System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to return elements from. count
- Type:
int
The number of elements to skip before returning the remaining elements.
Returns
Type: System.Collections.Generic.IEnumerable<TSource>
An System.Collections.Generic.IEnumerable<T> that contains the elements that occur after the specified index in the input sequence.
Exceptions
ArgumentNullException: source
is null.
ArgumentOutOfRangeException: count
is negative.
Examples
The following code example demonstrates how to use the Skip method to skip a specified number of elements from a list of integers.
// Sample data
List<int> numbers = new List<int> { 5, 4, 2, 9, 3, 10, 8 };
// Skip the first 3 elements
IEnumerable<int> remainingNumbers = numbers.Skip(3);
// Output the remaining elements
Console.WriteLine("Elements after skipping the first 3:");
foreach (int number in remainingNumbers)
{
Console.WriteLine(number);
}
// Expected output:
// Elements after skipping the first 3:
// 9
// 3
// 10
// 8
Remarks
If count
is less than or equal to zero, the original sequence is returned.
If count
is greater than the number of elements in source
, an empty sequence is returned.
The Skip
method is implemented by using deferred execution.