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