ElementAt.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #if REFERENCE_ASSEMBLY
  10. public static partial class AsyncEnumerableDeprecated
  11. #else
  12. public static partial class AsyncEnumerable
  13. #endif
  14. {
  15. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  16. // 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)
  17. /// <summary>
  18. /// Returns the element at a specified index in a sequence.
  19. /// </summary>
  20. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  21. /// <param name="source">async-enumerable sequence to return the element from.</param>
  22. /// <param name="index">The zero-based index of the element to retrieve.</param>
  23. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  24. /// <returns>An async-enumerable sequence that produces the element at the specified position in the source sequence.</returns>
  25. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  26. /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than zero.</exception>
  27. /// <exception cref="ArgumentOutOfRangeException">(Asynchronous) <paramref name="index"/> is greater than or equal to the number of elements in the source sequence.</exception>
  28. public static ValueTask<TSource> ElementAtAsync<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
  29. {
  30. if (source == null)
  31. throw Error.ArgumentNull(nameof(source));
  32. return Core(source, index, cancellationToken);
  33. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  34. {
  35. if (source is IAsyncPartition<TSource> p)
  36. {
  37. var first = await p.TryGetElementAtAsync(index, cancellationToken).ConfigureAwait(false);
  38. if (first.HasValue)
  39. {
  40. return first.Value;
  41. }
  42. }
  43. else
  44. {
  45. if (source is IList<TSource> list)
  46. {
  47. return list[index];
  48. }
  49. if (index >= 0)
  50. {
  51. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  52. {
  53. if (index == 0)
  54. {
  55. return item;
  56. }
  57. index--;
  58. }
  59. }
  60. }
  61. // NB: Even though index is captured, no closure is created.
  62. // The nameof expression is lowered to a literal prior to creating closures.
  63. throw Error.ArgumentOutOfRange(nameof(index));
  64. }
  65. }
  66. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  67. }
  68. }