// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; namespace System.Linq { public static partial class EnumerableEx { /// /// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds. /// /// Result sequence element type. /// Loop condition. /// Sequence to repeat while the condition evaluates true. /// Sequence generated by repeating the given sequence while the condition evaluates to true. public static IEnumerable While(Func condition, IEnumerable source) { if (condition == null) throw new ArgumentNullException(nameof(condition)); if (source == null) throw new ArgumentNullException(nameof(source)); return WhileCore(condition, source).Concat(); } private static IEnumerable> WhileCore(Func condition, IEnumerable source) { while (condition()) { yield return source; } } } }