فهرست منبع

Use using declarations.

Bart De Smet 6 سال پیش
والد
کامیت
550db9d840
21فایلهای تغییر یافته به همراه571 افزوده شده و 630 حذف شده
  1. 18 19
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Catch.cs
  2. 66 70
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs
  3. 73 76
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Do.cs
  4. 3 4
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/IsEmpty.cs
  5. 13 14
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Max.cs
  6. 13 14
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Min.cs
  7. 30 33
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Scan.cs
  8. 19 20
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Timeout.cs
  9. 12 15
      Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Using.cs
  10. 36 37
      Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerableHelpers.cs
  11. 33 36
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Aggregate.cs
  12. 3 4
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Any.cs
  13. 4 5
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/AppendPrepend.cs
  14. 30 33
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/GroupJoin.cs
  15. 54 57
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Join.cs
  16. 32 34
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/OrderedAsyncEnumerable.cs
  17. 8 11
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SequenceEqual.cs
  18. 11 12
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs
  19. 12 13
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs
  20. 81 90
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipWhile.cs
  21. 20 33
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Zip.cs

+ 18 - 19
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Catch.cs

@@ -216,33 +216,32 @@ namespace System.Linq
 
                 foreach (var source in sources)
                 {
-                    await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                    await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                    error = null;
+
+                    while (true)
                     {
-                        error = null;
+                        TSource c;
 
-                        while (true)
+                        try
                         {
-                            TSource c;
-
-                            try
-                            {
-                                if (!await e.MoveNextAsync())
-                                    break;
-
-                                c = e.Current;
-                            }
-                            catch (Exception ex)
-                            {
-                                error = ExceptionDispatchInfo.Capture(ex);
+                            if (!await e.MoveNextAsync())
                                 break;
-                            }
 
-                            yield return c;
+                            c = e.Current;
                         }
-
-                        if (error == null)
+                        catch (Exception ex)
+                        {
+                            error = ExceptionDispatchInfo.Capture(ex);
                             break;
+                        }
+
+                        yield return c;
                     }
+
+                    if (error == null)
+                        break;
                 }
 
                 error?.Throw();

+ 66 - 70
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/DistinctUntilChanged.cs

@@ -98,29 +98,28 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var latest = e.Current;
+                var latest = e.Current;
 
-                    yield return latest;
+                yield return latest;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        var item = e.Current;
+                while (await e.MoveNextAsync())
+                {
+                    var item = e.Current;
 
-                        // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
+                    // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
 
-                        if (!comparer!.Equals(latest, item))
-                        {
-                            latest = item;
+                    if (!comparer!.Equals(latest, item))
+                    {
+                        latest = item;
 
-                            yield return latest;
-                        }
+                        yield return latest;
                     }
                 }
             }
@@ -134,33 +133,32 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var item = e.Current;
+                var item = e.Current;
 
-                    var latestKey = keySelector(item);
+                var latestKey = keySelector(item);
 
-                    yield return item;
+                yield return item;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        item = e.Current;
+                while (await e.MoveNextAsync())
+                {
+                    item = e.Current;
 
-                        var currentKey = keySelector(item);
+                    var currentKey = keySelector(item);
 
-                        // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
+                    // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
 
-                        if (!comparer!.Equals(latestKey, currentKey))
-                        {
-                            latestKey = currentKey;
+                    if (!comparer!.Equals(latestKey, currentKey))
+                    {
+                        latestKey = currentKey;
 
-                            yield return item;
-                        }
+                        yield return item;
                     }
                 }
             }
@@ -174,33 +172,32 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var item = e.Current;
+                var item = e.Current;
 
-                    var latestKey = await keySelector(item).ConfigureAwait(false);
+                var latestKey = await keySelector(item).ConfigureAwait(false);
 
-                    yield return item;
+                yield return item;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        item = e.Current;
+                while (await e.MoveNextAsync())
+                {
+                    item = e.Current;
 
-                        var currentKey = await keySelector(item).ConfigureAwait(false);
+                    var currentKey = await keySelector(item).ConfigureAwait(false);
 
-                        // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
+                    // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
 
-                        if (!comparer!.Equals(latestKey, currentKey))
-                        {
-                            latestKey = currentKey;
+                    if (!comparer!.Equals(latestKey, currentKey))
+                    {
+                        latestKey = currentKey;
 
-                            yield return item;
-                        }
+                        yield return item;
                     }
                 }
             }
@@ -215,33 +212,32 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var item = e.Current;
+                var item = e.Current;
 
-                    var latestKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
+                var latestKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
 
-                    yield return item;
+                yield return item;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        item = e.Current;
+                while (await e.MoveNextAsync())
+                {
+                    item = e.Current;
 
-                        var currentKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
+                    var currentKey = await keySelector(item, cancellationToken).ConfigureAwait(false);
 
-                        // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
+                    // REVIEW: Need comparer!.Equals to satisfy nullable reference type warnings.
 
-                        if (!comparer!.Equals(latestKey, currentKey))
-                        {
-                            latestKey = currentKey;
+                    if (!comparer!.Equals(latestKey, currentKey))
+                    {
+                        latestKey = currentKey;
 
-                            yield return item;
-                        }
+                        yield return item;
                     }
                 }
             }

+ 73 - 76
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Do.cs

@@ -174,38 +174,37 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    while (true)
-                    {
-                        TSource item;
-
-                        try
-                        {
-                            if (!await e.MoveNextAsync())
-                            {
-                                break;
-                            }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                            item = e.Current;
+                while (true)
+                {
+                    TSource item;
 
-                            onNext(item);
-                        }
-                        catch (OperationCanceledException)
-                        {
-                            throw;
-                        }
-                        catch (Exception ex) when (onError != null)
+                    try
+                    {
+                        if (!await e.MoveNextAsync())
                         {
-                            onError(ex);
-                            throw;
+                            break;
                         }
 
-                        yield return item;
+                        item = e.Current;
+
+                        onNext(item);
+                    }
+                    catch (OperationCanceledException)
+                    {
+                        throw;
+                    }
+                    catch (Exception ex) when (onError != null)
+                    {
+                        onError(ex);
+                        throw;
                     }
 
-                    onCompleted?.Invoke();
+                    yield return item;
                 }
+
+                onCompleted?.Invoke();
             }
         }
 
@@ -215,40 +214,39 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    while (true)
-                    {
-                        TSource item;
-
-                        try
-                        {
-                            if (!await e.MoveNextAsync())
-                            {
-                                break;
-                            }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                            item = e.Current;
+                while (true)
+                {
+                    TSource item;
 
-                            await onNext(item).ConfigureAwait(false);
-                        }
-                        catch (OperationCanceledException)
-                        {
-                            throw;
-                        }
-                        catch (Exception ex) when (onError != null)
+                    try
+                    {
+                        if (!await e.MoveNextAsync())
                         {
-                            await onError(ex).ConfigureAwait(false);
-                            throw;
+                            break;
                         }
 
-                        yield return item;
-                    }
+                        item = e.Current;
 
-                    if (onCompleted != null)
+                        await onNext(item).ConfigureAwait(false);
+                    }
+                    catch (OperationCanceledException)
+                    {
+                        throw;
+                    }
+                    catch (Exception ex) when (onError != null)
                     {
-                        await onCompleted().ConfigureAwait(false);
+                        await onError(ex).ConfigureAwait(false);
+                        throw;
                     }
+
+                    yield return item;
+                }
+
+                if (onCompleted != null)
+                {
+                    await onCompleted().ConfigureAwait(false);
                 }
             }
         }
@@ -260,40 +258,39 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    while (true)
-                    {
-                        TSource item;
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                        try
-                        {
-                            if (!await e.MoveNextAsync())
-                            {
-                                break;
-                            }
-
-                            item = e.Current;
+                while (true)
+                {
+                    TSource item;
 
-                            await onNext(item, cancellationToken).ConfigureAwait(false);
-                        }
-                        catch (OperationCanceledException)
-                        {
-                            throw;
-                        }
-                        catch (Exception ex) when (onError != null)
+                    try
+                    {
+                        if (!await e.MoveNextAsync())
                         {
-                            await onError(ex, cancellationToken).ConfigureAwait(false);
-                            throw;
+                            break;
                         }
 
-                        yield return item;
-                    }
+                        item = e.Current;
 
-                    if (onCompleted != null)
+                        await onNext(item, cancellationToken).ConfigureAwait(false);
+                    }
+                    catch (OperationCanceledException)
                     {
-                        await onCompleted(cancellationToken).ConfigureAwait(false);
+                        throw;
                     }
+                    catch (Exception ex) when (onError != null)
+                    {
+                        await onError(ex, cancellationToken).ConfigureAwait(false);
+                        throw;
+                    }
+
+                    yield return item;
+                }
+
+                if (onCompleted != null)
+                {
+                    await onCompleted(cancellationToken).ConfigureAwait(false);
                 }
             }
         }

+ 3 - 4
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/IsEmpty.cs

@@ -19,10 +19,9 @@ namespace System.Linq
 
             static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    return !await e.MoveNextAsync();
-                }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                return !await e.MoveNextAsync();
             }
         }
     }

+ 13 - 14
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Max.cs

@@ -21,25 +21,24 @@ namespace System.Linq
             {
                 comparer ??= Comparer<TSource>.Default;
 
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    if (!await e.MoveNextAsync())
-                        throw Error.NoElements();
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                    var max = e.Current;
+                if (!await e.MoveNextAsync())
+                    throw Error.NoElements();
 
-                    while (await e.MoveNextAsync())
-                    {
-                        var cur = e.Current;
+                var max = e.Current;
 
-                        if (comparer.Compare(cur, max) > 0)
-                        {
-                            max = cur;
-                        }
-                    }
+                while (await e.MoveNextAsync())
+                {
+                    var cur = e.Current;
 
-                    return max;
+                    if (comparer.Compare(cur, max) > 0)
+                    {
+                        max = cur;
+                    }
                 }
+
+                return max;
             }
         }
     }

+ 13 - 14
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Min.cs

@@ -21,25 +21,24 @@ namespace System.Linq
             {
                 comparer ??= Comparer<TSource>.Default;
 
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    if (!await e.MoveNextAsync())
-                        throw Error.NoElements();
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                    var min = e.Current;
+                if (!await e.MoveNextAsync())
+                    throw Error.NoElements();
 
-                    while (await e.MoveNextAsync())
-                    {
-                        var cur = e.Current;
+                var min = e.Current;
 
-                        if (comparer.Compare(cur, min) < 0)
-                        {
-                            min = cur;
-                        }
-                    }
+                while (await e.MoveNextAsync())
+                {
+                    var cur = e.Current;
 
-                    return min;
+                    if (comparer.Compare(cur, min) < 0)
+                    {
+                        min = cur;
+                    }
                 }
+
+                return min;
             }
         }
     }

+ 30 - 33
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Scan.cs

@@ -25,21 +25,20 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var res = e.Current;
+                var res = e.Current;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        res = accumulator(res, e.Current);
+                while (await e.MoveNextAsync())
+                {
+                    res = accumulator(res, e.Current);
 
-                        yield return res;
-                    }
+                    yield return res;
                 }
             }
         }
@@ -77,21 +76,20 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var res = e.Current;
+                var res = e.Current;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        res = await accumulator(res, e.Current).ConfigureAwait(false);
+                while (await e.MoveNextAsync())
+                {
+                    res = await accumulator(res, e.Current).ConfigureAwait(false);
 
-                        yield return res;
-                    }
+                    yield return res;
                 }
             }
         }
@@ -108,21 +106,20 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        yield break;
-                    }
+                    yield break;
+                }
 
-                    var res = e.Current;
+                var res = e.Current;
 
-                    while (await e.MoveNextAsync())
-                    {
-                        res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
+                while (await e.MoveNextAsync())
+                {
+                    res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
 
-                        yield return res;
-                    }
+                    yield return res;
                 }
             }
         }

+ 19 - 20
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Timeout.cs

@@ -77,35 +77,34 @@ namespace System.Linq
 
                         if (!moveNext.IsCompleted)
                         {
-                            using (var delayCts = new CancellationTokenSource())
-                            {
-                                var delay = Task.Delay(_timeout, delayCts.Token);
+                            using var delayCts = new CancellationTokenSource();
 
-                                var next = moveNext.AsTask();
+                            var delay = Task.Delay(_timeout, delayCts.Token);
 
-                                var winner = await Task.WhenAny(next, delay).ConfigureAwait(false);
+                            var next = moveNext.AsTask();
 
-                                if (winner == delay)
-                                {
-                                    // NB: We still have to wait for the MoveNextAsync operation to complete before we can
-                                    //     dispose _enumerator. The resulting task will be used by DisposeAsync. Also note
-                                    //     that throwing an exception here causes a call to DisposeAsync, where we pick up
-                                    //     the task prepared below.
+                            var winner = await Task.WhenAny(next, delay).ConfigureAwait(false);
 
-                                    // NB: Any exception reported by a timed out MoveNextAsync operation won't be reported
-                                    //     to the caller, but the task's exception is not marked as observed, so unhandled
-                                    //     exception handlers can still observe the exception.
+                            if (winner == delay)
+                            {
+                                // NB: We still have to wait for the MoveNextAsync operation to complete before we can
+                                //     dispose _enumerator. The resulting task will be used by DisposeAsync. Also note
+                                //     that throwing an exception here causes a call to DisposeAsync, where we pick up
+                                //     the task prepared below.
 
-                                    // REVIEW: Should exceptions reported by a timed out MoveNextAsync operation come out
-                                    //         when attempting to call DisposeAsync?
+                                // NB: Any exception reported by a timed out MoveNextAsync operation won't be reported
+                                //     to the caller, but the task's exception is not marked as observed, so unhandled
+                                //     exception handlers can still observe the exception.
 
-                                    _loserTask = next.ContinueWith((_, state) => ((IAsyncDisposable)state).DisposeAsync().AsTask(), _enumerator);
+                                // REVIEW: Should exceptions reported by a timed out MoveNextAsync operation come out
+                                //         when attempting to call DisposeAsync?
 
-                                    throw new TimeoutException();
-                                }
+                                _loserTask = next.ContinueWith((_, state) => ((IAsyncDisposable)state).DisposeAsync().AsTask(), _enumerator);
 
-                                delayCts.Cancel();
+                                throw new TimeoutException();
                             }
+
+                            delayCts.Cancel();
                         }
 
                         if (await moveNext.ConfigureAwait(false))

+ 12 - 15
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Using.cs

@@ -23,12 +23,11 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                using (var resource = resourceFactory())
+                using var resource = resourceFactory();
+
+                await foreach (var item in enumerableFactory(resource).WithCancellation(cancellationToken).ConfigureAwait(false))
                 {
-                    await foreach (var item in enumerableFactory(resource).WithCancellation(cancellationToken).ConfigureAwait(false))
-                    {
-                        yield return item;
-                    }
+                    yield return item;
                 }
             }
         }
@@ -44,12 +43,11 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                using (var resource = await resourceFactory().ConfigureAwait(false))
+                using var resource = await resourceFactory().ConfigureAwait(false);
+
+                await foreach (var item in (await enumerableFactory(resource).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
                 {
-                    await foreach (var item in (await enumerableFactory(resource).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
-                    {
-                        yield return item;
-                    }
+                    yield return item;
                 }
             }
         }
@@ -66,12 +64,11 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                using (var resource = await resourceFactory(cancellationToken).ConfigureAwait(false))
+                using var resource = await resourceFactory(cancellationToken).ConfigureAwait(false);
+
+                await foreach (var item in (await enumerableFactory(resource, cancellationToken).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
                 {
-                    await foreach (var item in (await enumerableFactory(resource, cancellationToken).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
-                    {
-                        yield return item;
-                    }
+                    yield return item;
                 }
             }
         }

+ 36 - 37
Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerableHelpers.cs

@@ -44,52 +44,51 @@ namespace System.Collections.Generic
             }
             else
             {
-                await using (var en = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var en = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await en.MoveNextAsync())
                 {
-                    if (await en.MoveNextAsync())
-                    {
-                        const int DefaultCapacity = 4;
-                        var arr = new T[DefaultCapacity];
-                        arr[0] = en.Current;
-                        var count = 1;
+                    const int DefaultCapacity = 4;
+                    var arr = new T[DefaultCapacity];
+                    arr[0] = en.Current;
+                    var count = 1;
 
-                        while (await en.MoveNextAsync())
+                    while (await en.MoveNextAsync())
+                    {
+                        if (count == arr.Length)
                         {
-                            if (count == arr.Length)
-                            {
-                                // MaxArrayLength is defined in Array.MaxArrayLength and in gchelpers in CoreCLR.
-                                // It represents the maximum number of elements that can be in an array where
-                                // the size of the element is greater than one byte; a separate, slightly larger constant,
-                                // is used when the size of the element is one.
-                                const int MaxArrayLength = 0x7FEFFFFF;
-
-                                // This is the same growth logic as in List<T>:
-                                // If the array is currently empty, we make it a default size.  Otherwise, we attempt to 
-                                // double the size of the array.  Doubling will overflow once the size of the array reaches
-                                // 2^30, since doubling to 2^31 is 1 larger than Int32.MaxValue.  In that case, we instead 
-                                // constrain the length to be MaxArrayLength (this overflow check works because of the 
-                                // cast to uint).  Because a slightly larger constant is used when T is one byte in size, we 
-                                // could then end up in a situation where arr.Length is MaxArrayLength or slightly larger, such 
-                                // that we constrain newLength to be MaxArrayLength but the needed number of elements is actually 
-                                // larger than that.  For that case, we then ensure that the newLength is large enough to hold 
-                                // the desired capacity.  This does mean that in the very rare case where we've grown to such a 
-                                // large size, each new element added after MaxArrayLength will end up doing a resize.
-                                var newLength = count << 1;
-                                if ((uint)newLength > MaxArrayLength)
-                                {
-                                    newLength = MaxArrayLength <= count ? count + 1 : MaxArrayLength;
-                                }
+                            // MaxArrayLength is defined in Array.MaxArrayLength and in gchelpers in CoreCLR.
+                            // It represents the maximum number of elements that can be in an array where
+                            // the size of the element is greater than one byte; a separate, slightly larger constant,
+                            // is used when the size of the element is one.
+                            const int MaxArrayLength = 0x7FEFFFFF;
 
-                                Array.Resize(ref arr, newLength);
+                            // This is the same growth logic as in List<T>:
+                            // If the array is currently empty, we make it a default size.  Otherwise, we attempt to 
+                            // double the size of the array.  Doubling will overflow once the size of the array reaches
+                            // 2^30, since doubling to 2^31 is 1 larger than Int32.MaxValue.  In that case, we instead 
+                            // constrain the length to be MaxArrayLength (this overflow check works because of the 
+                            // cast to uint).  Because a slightly larger constant is used when T is one byte in size, we 
+                            // could then end up in a situation where arr.Length is MaxArrayLength or slightly larger, such 
+                            // that we constrain newLength to be MaxArrayLength but the needed number of elements is actually 
+                            // larger than that.  For that case, we then ensure that the newLength is large enough to hold 
+                            // the desired capacity.  This does mean that in the very rare case where we've grown to such a 
+                            // large size, each new element added after MaxArrayLength will end up doing a resize.
+                            var newLength = count << 1;
+                            if ((uint)newLength > MaxArrayLength)
+                            {
+                                newLength = MaxArrayLength <= count ? count + 1 : MaxArrayLength;
                             }
 
-                            arr[count++] = en.Current;
+                            Array.Resize(ref arr, newLength);
                         }
 
-                        result.Length = count;
-                        result.Array = arr;
-                        return result;
+                        arr[count++] = en.Current;
                     }
+
+                    result.Length = count;
+                    result.Array = arr;
+                    return result;
                 }
             }
 

+ 33 - 36
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Aggregate.cs

@@ -21,22 +21,21 @@ namespace System.Linq
 
             static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    if (!await e.MoveNextAsync())
-                    {
-                        throw Error.NoElements();
-                    }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                    var acc = e.Current;
+                if (!await e.MoveNextAsync())
+                {
+                    throw Error.NoElements();
+                }
 
-                    while (await e.MoveNextAsync())
-                    {
-                        acc = accumulator(acc, e.Current);
-                    }
+                var acc = e.Current;
 
-                    return acc;
+                while (await e.MoveNextAsync())
+                {
+                    acc = accumulator(acc, e.Current);
                 }
+
+                return acc;
             }
         }
 
@@ -51,22 +50,21 @@ namespace System.Linq
 
             static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    if (!await e.MoveNextAsync())
-                    {
-                        throw Error.NoElements();
-                    }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                    var acc = e.Current;
+                if (!await e.MoveNextAsync())
+                {
+                    throw Error.NoElements();
+                }
 
-                    while (await e.MoveNextAsync())
-                    {
-                        acc = await accumulator(acc, e.Current).ConfigureAwait(false);
-                    }
+                var acc = e.Current;
 
-                    return acc;
+                while (await e.MoveNextAsync())
+                {
+                    acc = await accumulator(acc, e.Current).ConfigureAwait(false);
                 }
+
+                return acc;
             }
         }
 
@@ -82,22 +80,21 @@ namespace System.Linq
 
             static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    if (!await e.MoveNextAsync())
-                    {
-                        throw Error.NoElements();
-                    }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
 
-                    var acc = e.Current;
+                if (!await e.MoveNextAsync())
+                {
+                    throw Error.NoElements();
+                }
 
-                    while (await e.MoveNextAsync())
-                    {
-                        acc = await accumulator(acc, e.Current, cancellationToken).ConfigureAwait(false);
-                    }
+                var acc = e.Current;
 
-                    return acc;
+                while (await e.MoveNextAsync())
+                {
+                    acc = await accumulator(acc, e.Current, cancellationToken).ConfigureAwait(false);
                 }
+
+                return acc;
             }
         }
 #endif

+ 3 - 4
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Any.cs

@@ -19,10 +19,9 @@ namespace System.Linq
 
             static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    return await e.MoveNextAsync();
-                }
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                return await e.MoveNextAsync();
             }
         }
 

+ 4 - 5
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/AppendPrepend.cs

@@ -428,12 +428,11 @@ namespace System.Linq
 
                 if (_appended != null)
                 {
-                    using (var en2 = _appended.GetEnumerator(_appendCount))
+                    using var en2 = _appended.GetEnumerator(_appendCount);
+
+                    while (en2.MoveNext())
                     {
-                        while (en2.MoveNext())
-                        {
-                            list.Add(en2.Current);
-                        }
+                        list.Add(en2.Current);
                     }
                 }
 

+ 30 - 33
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/GroupJoin.cs

@@ -30,20 +30,19 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await e.MoveNextAsync())
                 {
-                    if (await e.MoveNextAsync())
+                    var lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+
+                    do
                     {
-                        var lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
-
-                        do
-                        {
-                            var item = e.Current;
-                            var outerKey = outerKeySelector(item);
-                            yield return resultSelector(item, lookup[outerKey].ToAsyncEnumerable());
-                        }
-                        while (await e.MoveNextAsync());
+                        var item = e.Current;
+                        var outerKey = outerKeySelector(item);
+                        yield return resultSelector(item, lookup[outerKey].ToAsyncEnumerable());
                     }
+                    while (await e.MoveNextAsync());
                 }
             }
         }
@@ -68,20 +67,19 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await e.MoveNextAsync())
                 {
-                    if (await e.MoveNextAsync())
+                    var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+
+                    do
                     {
-                        var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
-
-                        do
-                        {
-                            var item = e.Current;
-                            var outerKey = await outerKeySelector(item).ConfigureAwait(false);
-                            yield return await resultSelector(item, lookup[outerKey].ToAsyncEnumerable()).ConfigureAwait(false);
-                        }
-                        while (await e.MoveNextAsync());
+                        var item = e.Current;
+                        var outerKey = await outerKeySelector(item).ConfigureAwait(false);
+                        yield return await resultSelector(item, lookup[outerKey].ToAsyncEnumerable()).ConfigureAwait(false);
                     }
+                    while (await e.MoveNextAsync());
                 }
             }
         }
@@ -107,20 +105,19 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await e.MoveNextAsync())
                 {
-                    if (await e.MoveNextAsync())
+                    var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+
+                    do
                     {
-                        var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
-
-                        do
-                        {
-                            var item = e.Current;
-                            var outerKey = await outerKeySelector(item, cancellationToken).ConfigureAwait(false);
-                            yield return await resultSelector(item, lookup[outerKey].ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
-                        }
-                        while (await e.MoveNextAsync());
+                        var item = e.Current;
+                        var outerKey = await outerKeySelector(item, cancellationToken).ConfigureAwait(false);
+                        yield return await resultSelector(item, lookup[outerKey].ToAsyncEnumerable(), cancellationToken).ConfigureAwait(false);
                     }
+                    while (await e.MoveNextAsync());
                 }
             }
         }

+ 54 - 57
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Join.cs

@@ -30,35 +30,34 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await e.MoveNextAsync())
                 {
-                    if (await e.MoveNextAsync())
-                    {
-                        var lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+                    var lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
 
-                        if (lookup.Count != 0)
+                    if (lookup.Count != 0)
+                    {
+                        do
                         {
-                            do
-                            {
-                                var item = e.Current;
+                            var item = e.Current;
 
-                                var outerKey = outerKeySelector(item);
+                            var outerKey = outerKeySelector(item);
 
-                                var g = lookup.GetGrouping(outerKey);
+                            var g = lookup.GetGrouping(outerKey);
 
-                                if (g != null)
-                                {
-                                    var count = g._count;
-                                    var elements = g._elements;
+                            if (g != null)
+                            {
+                                var count = g._count;
+                                var elements = g._elements;
 
-                                    for (var i = 0; i != count; ++i)
-                                    {
-                                        yield return resultSelector(item, elements[i]);
-                                    }
+                                for (var i = 0; i != count; ++i)
+                                {
+                                    yield return resultSelector(item, elements[i]);
                                 }
                             }
-                            while (await e.MoveNextAsync());
                         }
+                        while (await e.MoveNextAsync());
                     }
                 }
             }
@@ -84,35 +83,34 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await e.MoveNextAsync())
                 {
-                    if (await e.MoveNextAsync())
-                    {
-                        var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+                    var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
 
-                        if (lookup.Count != 0)
+                    if (lookup.Count != 0)
+                    {
+                        do
                         {
-                            do
-                            {
-                                var item = e.Current;
+                            var item = e.Current;
 
-                                var outerKey = await outerKeySelector(item).ConfigureAwait(false);
+                            var outerKey = await outerKeySelector(item).ConfigureAwait(false);
 
-                                var g = lookup.GetGrouping(outerKey);
+                            var g = lookup.GetGrouping(outerKey);
 
-                                if (g != null)
-                                {
-                                    var count = g._count;
-                                    var elements = g._elements;
+                            if (g != null)
+                            {
+                                var count = g._count;
+                                var elements = g._elements;
 
-                                    for (var i = 0; i != count; ++i)
-                                    {
-                                        yield return await resultSelector(item, elements[i]).ConfigureAwait(false);
-                                    }
+                                for (var i = 0; i != count; ++i)
+                                {
+                                    yield return await resultSelector(item, elements[i]).ConfigureAwait(false);
                                 }
                             }
-                            while (await e.MoveNextAsync());
                         }
+                        while (await e.MoveNextAsync());
                     }
                 }
             }
@@ -139,35 +137,34 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = outer.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (await e.MoveNextAsync())
                 {
-                    if (await e.MoveNextAsync())
-                    {
-                        var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+                    var lookup = await Internal.LookupWithTask<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
 
-                        if (lookup.Count != 0)
+                    if (lookup.Count != 0)
+                    {
+                        do
                         {
-                            do
-                            {
-                                var item = e.Current;
+                            var item = e.Current;
 
-                                var outerKey = await outerKeySelector(item, cancellationToken).ConfigureAwait(false);
+                            var outerKey = await outerKeySelector(item, cancellationToken).ConfigureAwait(false);
 
-                                var g = lookup.GetGrouping(outerKey);
+                            var g = lookup.GetGrouping(outerKey);
 
-                                if (g != null)
-                                {
-                                    var count = g._count;
-                                    var elements = g._elements;
+                            if (g != null)
+                            {
+                                var count = g._count;
+                                var elements = g._elements;
 
-                                    for (var i = 0; i != count; ++i)
-                                    {
-                                        yield return await resultSelector(item, elements[i], cancellationToken).ConfigureAwait(false);
-                                    }
+                                for (var i = 0; i != count; ++i)
+                                {
+                                    yield return await resultSelector(item, elements[i], cancellationToken).ConfigureAwait(false);
                                 }
                             }
-                            while (await e.MoveNextAsync());
                         }
+                        while (await e.MoveNextAsync());
                     }
                 }
             }

+ 32 - 34
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/OrderedAsyncEnumerable.cs

@@ -264,60 +264,58 @@ namespace System.Linq
 
         public async ValueTask<Maybe<TElement>> TryGetFirstAsync(CancellationToken cancellationToken)
         {
-            await using (var e = _source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+            await using var e = _source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+            if (!await e.MoveNextAsync())
             {
-                if (!await e.MoveNextAsync())
-                {
-                    return new Maybe<TElement>();
-                }
+                return new Maybe<TElement>();
+            }
 
-                var value = e.Current;
+            var value = e.Current;
+
+            var comparer = GetComparer();
 
-                var comparer = GetComparer();
+            await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
 
-                await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
+            while (await e.MoveNextAsync())
+            {
+                var x = e.Current;
 
-                while (await e.MoveNextAsync())
+                if (await comparer.Compare(x, cacheLower: true, cancellationToken).ConfigureAwait(false) < 0)
                 {
-                    var x = e.Current;
-
-                    if (await comparer.Compare(x, cacheLower: true, cancellationToken).ConfigureAwait(false) < 0)
-                    {
-                        value = x;
-                    }
+                    value = x;
                 }
-
-                return new Maybe<TElement>(value);
             }
+
+            return new Maybe<TElement>(value);
         }
 
         public async ValueTask<Maybe<TElement>> TryGetLastAsync(CancellationToken cancellationToken)
         {
-            await using (var e = _source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+            await using var e = _source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+            if (!await e.MoveNextAsync())
             {
-                if (!await e.MoveNextAsync())
-                {
-                    return new Maybe<TElement>();
-                }
+                return new Maybe<TElement>();
+            }
 
-                var value = e.Current;
+            var value = e.Current;
 
-                var comparer = GetComparer();
+            var comparer = GetComparer();
 
-                await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
+            await comparer.SetElement(value, cancellationToken).ConfigureAwait(false);
 
-                while (await e.MoveNextAsync())
-                {
-                    var current = e.Current;
+            while (await e.MoveNextAsync())
+            {
+                var current = e.Current;
 
-                    if (await comparer.Compare(current, cacheLower: false, cancellationToken).ConfigureAwait(false) >= 0)
-                    {
-                        value = current;
-                    }
+                if (await comparer.Compare(current, cacheLower: false, cancellationToken).ConfigureAwait(false) >= 0)
+                {
+                    value = current;
                 }
-
-                return new Maybe<TElement>(value);
             }
+
+            return new Maybe<TElement>(value);
         }
 
         internal async ValueTask<Maybe<TElement>> TryGetLastAsync(int minIndexInclusive, int maxIndexInclusive, CancellationToken cancellationToken)

+ 8 - 11
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SequenceEqual.cs

@@ -49,21 +49,18 @@ namespace System.Linq
 
             static async ValueTask<bool> Core(IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
             {
-                await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e1.MoveNextAsync())
                 {
-                    await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                    if (!(await e2.MoveNextAsync() && comparer.Equals(e1.Current, e2.Current)))
                     {
-                        while (await e1.MoveNextAsync())
-                        {
-                            if (!(await e2.MoveNextAsync() && comparer.Equals(e1.Current, e2.Current)))
-                            {
-                                return false;
-                            }
-                        }
-
-                        return !await e2.MoveNextAsync();
+                        return false;
                     }
                 }
+
+                return !await e2.MoveNextAsync();
             }
         }
     }

+ 11 - 12
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs

@@ -30,22 +30,21 @@ namespace System.Linq
                     throw Error.MoreThanOneElement();
                 }
 
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                if (!await e.MoveNextAsync())
                 {
-                    if (!await e.MoveNextAsync())
-                    {
-                        throw Error.NoElements();
-                    }
+                    throw Error.NoElements();
+                }
 
-                    var result = e.Current;
+                var result = e.Current;
 
-                    if (await e.MoveNextAsync())
-                    {
-                        throw Error.MoreThanOneElement();
-                    }
-
-                    return result;
+                if (await e.MoveNextAsync())
+                {
+                    throw Error.MoreThanOneElement();
                 }
+
+                return result;
             }
         }
 

+ 12 - 13
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs

@@ -34,24 +34,23 @@ namespace System.Linq
             {
                 var queue = new Queue<TSource>();
 
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e.MoveNextAsync())
                 {
-                    while (await e.MoveNextAsync())
+                    if (queue.Count == count)
                     {
-                        if (queue.Count == count)
-                        {
-                            do
-                            {
-                                yield return queue.Dequeue();
-                                queue.Enqueue(e.Current);
-                            }
-                            while (await e.MoveNextAsync());
-                            break;
-                        }
-                        else
+                        do
                         {
+                            yield return queue.Dequeue();
                             queue.Enqueue(e.Current);
                         }
+                        while (await e.MoveNextAsync());
+                        break;
+                    }
+                    else
+                    {
+                        queue.Enqueue(e.Current);
                     }
                 }
             }

+ 81 - 90
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipWhile.cs

@@ -21,23 +21,22 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e.MoveNextAsync())
                 {
-                    while (await e.MoveNextAsync())
+                    var element = e.Current;
+
+                    if (!predicate(element))
                     {
-                        var element = e.Current;
+                        yield return element;
 
-                        if (!predicate(element))
+                        while (await e.MoveNextAsync())
                         {
-                            yield return element;
-
-                            while (await e.MoveNextAsync())
-                            {
-                                yield return e.Current;
-                            }
-
-                            yield break;
+                            yield return e.Current;
                         }
+
+                        yield break;
                     }
                 }
             }
@@ -54,30 +53,28 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    var index = -1;
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                var index = -1;
 
-                    while (await e.MoveNextAsync())
+                while (await e.MoveNextAsync())
+                {
+                    checked
                     {
-                        checked
-                        {
-                            index++;
-                        }
-
-                        var element = e.Current;
+                        index++;
+                    }
 
-                        if (!predicate(element, index))
-                        {
-                            yield return element;
+                    var element = e.Current;
 
-                            while (await e.MoveNextAsync())
-                            {
-                                yield return e.Current;
-                            }
+                    if (!predicate(element, index))
+                    {
+                        yield return element;
 
-                            yield break;
+                        while (await e.MoveNextAsync())
+                        {
+                            yield return e.Current;
                         }
+
+                        yield break;
                     }
                 }
             }
@@ -94,23 +91,22 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e.MoveNextAsync())
                 {
-                    while (await e.MoveNextAsync())
+                    var element = e.Current;
+
+                    if (!await predicate(element).ConfigureAwait(false))
                     {
-                        var element = e.Current;
+                        yield return element;
 
-                        if (!await predicate(element).ConfigureAwait(false))
+                        while (await e.MoveNextAsync())
                         {
-                            yield return element;
-
-                            while (await e.MoveNextAsync())
-                            {
-                                yield return e.Current;
-                            }
-
-                            yield break;
+                            yield return e.Current;
                         }
+
+                        yield break;
                     }
                 }
             }
@@ -128,23 +124,22 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e.MoveNextAsync())
                 {
-                    while (await e.MoveNextAsync())
+                    var element = e.Current;
+
+                    if (!await predicate(element, cancellationToken).ConfigureAwait(false))
                     {
-                        var element = e.Current;
+                        yield return element;
 
-                        if (!await predicate(element, cancellationToken).ConfigureAwait(false))
+                        while (await e.MoveNextAsync())
                         {
-                            yield return element;
-
-                            while (await e.MoveNextAsync())
-                            {
-                                yield return e.Current;
-                            }
-
-                            yield break;
+                            yield return e.Current;
                         }
+
+                        yield break;
                     }
                 }
             }
@@ -162,30 +157,28 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    var index = -1;
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                var index = -1;
 
-                    while (await e.MoveNextAsync())
+                while (await e.MoveNextAsync())
+                {
+                    checked
                     {
-                        checked
-                        {
-                            index++;
-                        }
-
-                        var element = e.Current;
+                        index++;
+                    }
 
-                        if (!await predicate(element, index).ConfigureAwait(false))
-                        {
-                            yield return element;
+                    var element = e.Current;
 
-                            while (await e.MoveNextAsync())
-                            {
-                                yield return e.Current;
-                            }
+                    if (!await predicate(element, index).ConfigureAwait(false))
+                    {
+                        yield return element;
 
-                            yield break;
+                        while (await e.MoveNextAsync())
+                        {
+                            yield return e.Current;
                         }
+
+                        yield break;
                     }
                 }
             }
@@ -203,30 +196,28 @@ namespace System.Linq
 
             async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
             {
-                await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                {
-                    var index = -1;
+                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                var index = -1;
 
-                    while (await e.MoveNextAsync())
+                while (await e.MoveNextAsync())
+                {
+                    checked
                     {
-                        checked
-                        {
-                            index++;
-                        }
-
-                        var element = e.Current;
+                        index++;
+                    }
 
-                        if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
-                        {
-                            yield return element;
+                    var element = e.Current;
 
-                            while (await e.MoveNextAsync())
-                            {
-                                yield return e.Current;
-                            }
+                    if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
+                    {
+                        yield return element;
 
-                            yield break;
+                        while (await e.MoveNextAsync())
+                        {
+                            yield return e.Current;
                         }
+
+                        yield break;
                     }
                 }
             }

+ 20 - 33
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Zip.cs

@@ -3,7 +3,6 @@
 // See the LICENSE file in the project root for more information. 
 
 using System.Collections.Generic;
-using System.Diagnostics;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -23,15 +22,12 @@ namespace System.Linq
 
             async IAsyncEnumerator<(TFirst, TSecond)> Core(CancellationToken cancellationToken)
             {
-                await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                
+                while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
                 {
-                    await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                    {
-                        while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
-                        {
-                            yield return (e1.Current, e2.Current);
-                        }
-                    }
+                    yield return (e1.Current, e2.Current);
                 }
             }
         }
@@ -50,15 +46,12 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
                 {
-                    await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                    {
-                        while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
-                        {
-                            yield return selector(e1.Current, e2.Current);
-                        }
-                    }
+                    yield return selector(e1.Current, e2.Current);
                 }
             }
         }
@@ -76,15 +69,12 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
                 {
-                    await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                    {
-                        while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
-                        {
-                            yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
-                        }
-                    }
+                    yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
                 }
             }
         }
@@ -103,15 +93,12 @@ namespace System.Linq
 
             async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
             {
-                await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
+                await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
+                await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
+
+                while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
                 {
-                    await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
-                    {
-                        while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
-                        {
-                            yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
-                        }
-                    }
+                    yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
                 }
             }
         }