ElementAt.cs 3.5 KB

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