{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The TakeWhile operator yields elements from a sequence while a test is true and then skips the remainder of the sequence.
EditMethod Signatures
// 1 - Return elements until the predicate returns false.
public static IEnumerable<TSource> TakeWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
// 2 - Return elements until the predicate returns false.
// The predicate has access to the current elements index.
public static IEnumerable<TSource> TakeWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate)
EditExceptions
Throws an ArgumentNullException if
source or
predicate are null.
EditPseudo-code
Overload 1
If
source is null, throw an ArgumentNullException.
If
predicate is null, throw an ArgumentNullException.
Iterate the
source sequence.
If predicate(current element) returns false, then exit.
Return the current element. Resume execution from this point when the next element is requested.
Overload 2
If
source is null, throw an ArgumentNullException.
If
predicate is null, throw an ArgumentNullException.
Initialize a counter to zero.
Iterate the
source sequence.
If predicate(current element, counter) returns false, then exit.
Return the current element. Resume execution from this point when the next element is requested.
Increment the counter by one.
EditLoop Count
This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.
EditCode Samples
TODO: Needs code sample.