1
0

ToEnumerable.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 AsyncEnumerable
  8. {
  9. public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  10. {
  11. if (source == null)
  12. throw Error.ArgumentNull(nameof(source));
  13. return Core();
  14. IEnumerable<TSource> Core()
  15. {
  16. var e = source.GetAsyncEnumerator(default);
  17. try
  18. {
  19. while (true)
  20. {
  21. if (!e.MoveNextAsync().GetAwaiter().GetResult())
  22. break;
  23. yield return e.Current;
  24. }
  25. }
  26. finally
  27. {
  28. // Wait
  29. e.DisposeAsync().GetAwaiter().GetResult();
  30. }
  31. }
  32. }
  33. }
  34. }