METHOD

Enumerable.TakeWhile

Returns the initial consecutive elements satisfying the predicate, stopping at the first failure. Unlike Where, later matching elements are not returned.

Formal Syntax & Structure

csharp syntaxcsharp
items.TakeWhile(predicate)

Working Example

Example Snippetcsharp
using System;using System.Linq;Console.WriteLine(string.Join(",", new[] { 2, 4, 1, 6 }.TakeWhile(n => n % 2 == 0))); // 2,4
C# & .NET Reference