DoWhile.cs 1.2 KB

12345678910111213141516171819202122232425262728
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
  11. /// </summary>
  12. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  13. /// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
  14. /// <param name="condition">Loop condition.</param>
  15. /// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
  16. public static IEnumerable<TResult> DoWhile<TResult>(this IEnumerable<TResult> source, Func<bool> condition)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. if (condition == null)
  21. throw new ArgumentNullException(nameof(condition));
  22. return source.Concat(While(condition, source));
  23. }
  24. }
  25. }