AsyncEnumerable.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. using System.Threading.Tasks;
  6. namespace System.Linq
  7. {
  8. public static partial class AsyncEnumerable
  9. {
  10. public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  11. {
  12. if (source == null)
  13. throw new ArgumentNullException(nameof(source));
  14. return source.Select(x => x);
  15. }
  16. public static IAsyncEnumerable<TValue> Return<TValue>(TValue value)
  17. {
  18. return new[] { value }.ToAsyncEnumerable();
  19. }
  20. public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  21. {
  22. if (exception == null)
  23. throw new ArgumentNullException(nameof(exception));
  24. return CreateEnumerable(
  25. () => CreateEnumerator<TValue>(
  26. ct =>
  27. {
  28. var tcs = new TaskCompletionSource<bool>();
  29. tcs.TrySetException(exception);
  30. return tcs.Task;
  31. },
  32. current: null,
  33. dispose: null)
  34. );
  35. }
  36. }
  37. }