Method SkipUntil
| Edit this page View SourceSkipUntil<TSource>(IEnumerable<TSource>, Func<TSource, bool>)
Skips items from the input sequence until the given predicate returns true when applied to the current source item; that item will be the last skipped.
Declaration
public static IEnumerable<TSource> SkipUntil<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence |
Func<TSource, bool> | predicate | Predicate used to determine when to stop yielding results from the source. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | Items from the source sequence after the predicate first returns true when applied to the item. |
Type Parameters
Name | Description |
---|---|
TSource | Type of the source sequence |
Remarks
SkipUntil differs from SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource, bool>) in two respects.
Firstly, the sense of the predicate is reversed: it is expected that the predicate will return false to start with, and then return true - for example, when trying to find a matching item in a sequence.
Secondly, SkipUntil skips the element which causes the predicate to return true.
This operator uses deferred execution and streams its result.
Examples
The following code example demonstrates how to bypass elements in a sequence until a condition is true using SkipUntil
.
var sequence = Enumerable.Range(1, 10);
// Skip elements until the condition is true
var result = sequence
.SkipUntil(x => x == 5);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// One possible random output is as follows:
// [6, 7, 8, 9, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|