ElementAt.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.elementatasync?view=net-9.0-pp#system-linq-asyncenumerable-elementatasync-1(system-collections-generic-iasyncenumerable((-0))-system-int32-system-threading-cancellationtoken)
  13. /// <summary>
  14. /// Returns the element at a specified index in a sequence.
  15. /// </summary>
  16. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  17. /// <param name="source">async-enumerable sequence to return the element from.</param>
  18. /// <param name="index">The zero-based index of the element to retrieve.</param>
  19. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  20. /// <returns>An async-enumerable sequence that produces the element at the specified position in the source sequence.</returns>
  21. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  22. /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than zero.</exception>
  23. /// <exception cref="ArgumentOutOfRangeException">(Asynchronous) <paramref name="index"/> is greater than or equal to the number of elements in the source sequence.</exception>
  24. public static ValueTask<TSource> ElementAtAsync<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
  25. {
  26. if (source == null)
  27. throw Error.ArgumentNull(nameof(source));
  28. return Core(source, index, cancellationToken);
  29. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  30. {
  31. if (source is IAsyncPartition<TSource> p)
  32. {
  33. var first = await p.TryGetElementAtAsync(index, cancellationToken).ConfigureAwait(false);
  34. if (first.HasValue)
  35. {
  36. return first.Value;
  37. }
  38. }
  39. else
  40. {
  41. if (source is IList<TSource> list)
  42. {
  43. return list[index];
  44. }
  45. if (index >= 0)
  46. {
  47. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  48. {
  49. if (index == 0)
  50. {
  51. return item;
  52. }
  53. index--;
  54. }
  55. }
  56. }
  57. // NB: Even though index is captured, no closure is created.
  58. // The nameof expression is lowered to a literal prior to creating closures.
  59. throw Error.ArgumentOutOfRange(nameof(index));
  60. }
  61. }
  62. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  63. }
  64. }