ElementAt.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<TSource> ElementAt<TSource>(this IAsyncEnumerable<TSource> source, int index)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return ElementAt(source, index, CancellationToken.None);
  16. }
  17. public static Task<TSource> ElementAt<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (index < 0)
  22. throw new ArgumentOutOfRangeException(nameof(index));
  23. return ElementAt_(source, index, cancellationToken);
  24. }
  25. public static Task<TSource> ElementAtOrDefault<TSource>(this IAsyncEnumerable<TSource> source, int index)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. return ElementAtOrDefault(source, index, CancellationToken.None);
  30. }
  31. public static Task<TSource> ElementAtOrDefault<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (index < 0)
  36. throw new ArgumentOutOfRangeException(nameof(index));
  37. return ElementAtOrDefault_(source, index, cancellationToken);
  38. }
  39. private static async Task<TSource> ElementAt_<TSource>(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  40. {
  41. if (source is IList<TSource> list)
  42. {
  43. return list[index];
  44. }
  45. if (index >= 0)
  46. {
  47. var e = source.GetAsyncEnumerator();
  48. try
  49. {
  50. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  51. {
  52. if (index == 0)
  53. {
  54. return e.Current;
  55. }
  56. index--;
  57. }
  58. }
  59. finally
  60. {
  61. await e.DisposeAsync().ConfigureAwait(false);
  62. }
  63. }
  64. throw new ArgumentOutOfRangeException(nameof(index));
  65. }
  66. private static async Task<TSource> ElementAtOrDefault_<TSource>(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  67. {
  68. if (index >= 0)
  69. {
  70. var e = source.GetAsyncEnumerator();
  71. try
  72. {
  73. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  74. {
  75. if (index == 0)
  76. {
  77. return e.Current;
  78. }
  79. index--;
  80. }
  81. }
  82. finally
  83. {
  84. await e.DisposeAsync().ConfigureAwait(false);
  85. }
  86. }
  87. return default(TSource);
  88. }
  89. }
  90. }