CKTIS / C#
Enumerable.TakeWhile: practical reference
Enumerable.TakeWhile: practical reference: Returns the initial segment while the predicate is true and stops at the first false.
Version: .NET LINQ; verified on .NET 10.0.14
Syntax
source.TakeWhile(/* overload-specific arguments */)Returns the initial segment while the predicate is true and stops at the first false.
Use it to take a condition-bounded prefix, not to filter the whole sequence.
The first false element and every later element are omitted, even if the predicate would later become true again.
An empty source yields an empty result. A null source or predicate causes ArgumentNullException.
The complete program actually uses Enumerable.TakeWhile; its exact expected stdout is 1,2.
Examples
using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(string.Join(",", new[] { 1, 2, 5, 2 }.TakeWhile(value => value < 5)));
Result
1,2
The complete program actually uses `Enumerable.TakeWhile`; its exact expected stdout is `1,2`.