ElementAtOrDefault.cs 3.4 KB

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