|
|
@@ -14,8 +14,6 @@ namespace System.Linq
|
|
|
{
|
|
|
if (source == null)
|
|
|
throw Error.ArgumentNull(nameof(source));
|
|
|
- if (index < 0)
|
|
|
- throw Error.ArgumentOutOfRange(nameof(index));
|
|
|
|
|
|
return ElementAtOrDefaultCore(source, index, CancellationToken.None);
|
|
|
}
|
|
|
@@ -24,22 +22,13 @@ namespace System.Linq
|
|
|
{
|
|
|
if (source == null)
|
|
|
throw Error.ArgumentNull(nameof(source));
|
|
|
- if (index < 0)
|
|
|
- throw Error.ArgumentOutOfRange(nameof(index));
|
|
|
|
|
|
return ElementAtOrDefaultCore(source, index, cancellationToken);
|
|
|
}
|
|
|
|
|
|
private static async Task<TSource> ElementAtOrDefaultCore<TSource>(IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
|
|
|
{
|
|
|
- if (source is IList<TSource> list)
|
|
|
- {
|
|
|
- if (index < list.Count)
|
|
|
- {
|
|
|
- return list[index];
|
|
|
- }
|
|
|
- }
|
|
|
- else if (source is IAsyncPartition<TSource> p)
|
|
|
+ if (source is IAsyncPartition<TSource> p)
|
|
|
{
|
|
|
var first = await p.TryGetElementAsync(index, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
@@ -48,25 +37,36 @@ namespace System.Linq
|
|
|
return first.Value;
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- var e = source.GetAsyncEnumerator(cancellationToken);
|
|
|
|
|
|
- try
|
|
|
+ if (index >= 0)
|
|
|
+ {
|
|
|
+ if (source is IList<TSource> list)
|
|
|
{
|
|
|
- while (await e.MoveNextAsync().ConfigureAwait(false))
|
|
|
+ if (index < list.Count)
|
|
|
{
|
|
|
- if (index == 0)
|
|
|
- {
|
|
|
- return e.Current;
|
|
|
- }
|
|
|
-
|
|
|
- index--;
|
|
|
+ return list[index];
|
|
|
}
|
|
|
}
|
|
|
- finally
|
|
|
+ else
|
|
|
{
|
|
|
- await e.DisposeAsync().ConfigureAwait(false);
|
|
|
+ var e = source.GetAsyncEnumerator(cancellationToken);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ while (await e.MoveNextAsync().ConfigureAwait(false))
|
|
|
+ {
|
|
|
+ if (index == 0)
|
|
|
+ {
|
|
|
+ return e.Current;
|
|
|
+ }
|
|
|
+
|
|
|
+ index--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ await e.DisposeAsync().ConfigureAwait(false);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|