ElementAtOrDefault.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.elementatordefaultasync?view=net-9.0-pp#system-linq-asyncenumerable-elementatordefaultasync-1(system-collections-generic-iasyncenumerable((-0))-system-int32-system-threading-cancellationtoken)
  13. /// <summary>
  14. /// Returns the element at a specified index in a sequence or a default value if the index is out of range.
  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, or a default value if the index is outside the bounds of 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. public static ValueTask<TSource?> ElementAtOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken = default)
  24. {
  25. if (source == null)
  26. throw Error.ArgumentNull(nameof(source));
  27. return Core(source, index, cancellationToken);
  28. static async ValueTask<TSource?> Core(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  29. {
  30. if (source is IAsyncPartition<TSource> p)
  31. {
  32. var first = await p.TryGetElementAtAsync(index, cancellationToken).ConfigureAwait(false);
  33. if (first.HasValue)
  34. {
  35. return first.Value;
  36. }
  37. }
  38. if (index >= 0)
  39. {
  40. if (source is IList<TSource> list)
  41. {
  42. if (index < list.Count)
  43. {
  44. return list[index];
  45. }
  46. }
  47. else
  48. {
  49. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  50. {
  51. if (index == 0)
  52. {
  53. return item;
  54. }
  55. index--;
  56. }
  57. }
  58. }
  59. return default;
  60. }
  61. }
  62. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  63. }
  64. }