Bypasses a specified number of elements in a sequence and then returns the remaining elements.
public static IEnumerable<TSource> Skip<TSource>(
this IEnumerable<TSource> source,
int count
)
Name | Description |
---|---|
source |
An IEnumerable<TSource> to return elements from. |
count |
The number of elements to skip before returning the remaining elements. |
An IEnumerable<TSource>
that contains the elements that occur after the specified index in the input sequence.
count
is less than or equal to zero, Skip
returns all elements in the source
.count
is greater than the number of elements in source
, Skip
returns an empty sequence.Skip
method performs deferred execution.
using System;
using System.Collections.Generic;
using System.Linq;
public class SkipExample
{
public static void Main(string[] args)
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Skip the first 3 elements
IEnumerable<int> remainingNumbers = numbers.Skip(3);
Console.WriteLine("Original numbers:");
foreach (var number in numbers)
{
Console.Write($"{number} ");
}
Console.WriteLine("\n");
Console.WriteLine("Numbers after skipping the first 3:");
foreach (var number in remainingNumbers)
{
Console.Write($"{number} ");
}
// Output: 4 5 6 7 8 9 10
}
}