ElementAt.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. using (var e = source.GetEnumerator())
  50. {
  51. while (await e.MoveNext(cancellationToken)
  52. .ConfigureAwait(false))
  53. {
  54. if (index == 0)
  55. {
  56. return e.Current;
  57. }
  58. index--;
  59. }
  60. }
  61. }
  62. throw new ArgumentOutOfRangeException(nameof(index));
  63. }
  64. private static async Task<TSource> ElementAtOrDefault_<TSource>(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
  65. {
  66. if (index >= 0)
  67. {
  68. using (var e = source.GetEnumerator())
  69. {
  70. while (await e.MoveNext(cancellationToken)
  71. .ConfigureAwait(false))
  72. {
  73. if (index == 0)
  74. {
  75. return e.Current;
  76. }
  77. index--;
  78. }
  79. }
  80. }
  81. return default(TSource);
  82. }
  83. }
  84. }