1
0

AsyncEnumerable.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #if NO_TASK_FROMEXCEPTION
  25. var tcs = new TaskCompletionSource<bool>();
  26. tcs.TrySetException(exception);
  27. var moveNextThrows = tcs.Task;
  28. #else
  29. var moveNextThrows = Task.FromException<bool>(exception);
  30. #endif
  31. return CreateEnumerable(
  32. () => CreateEnumerator<TValue>(
  33. () => moveNextThrows,
  34. current: null,
  35. dispose: null)
  36. );
  37. }
  38. }
  39. }