浏览代码

Reduce loop bodies

Bart De Smet 6 年之前
父节点
当前提交
d6171d53ee

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

@@ -205,12 +205,12 @@ namespace System.Linq
 #if !NO_DEEP_CANCELLATION
         private static async Task<Dictionary<TKey, TElement>> ToDictionaryCore<TSource, TKey, TElement>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, Func<TSource, CancellationToken, ValueTask<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
         {
+            var d = new Dictionary<TKey, TElement>(comparer);
+
             var e = source.GetAsyncEnumerator(cancellationToken);
 
             try
             {
-                var d = new Dictionary<TKey, TElement>(comparer);
-
                 while (await e.MoveNextAsync().ConfigureAwait(false))
                 {
                     var x = e.Current;
@@ -220,13 +220,13 @@ namespace System.Linq
 
                     d.Add(key, value);
                 }
-
-                return d;
             }
             finally
             {
                 await e.DisposeAsync().ConfigureAwait(false);
             }
+
+            return d;
         }
 #endif
     }

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

@@ -28,23 +28,23 @@ namespace System.Linq
 
         private static async Task<HashSet<TSource>> ToHashSetCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
         {
+            var set = new HashSet<TSource>(comparer);
+
             var e = source.GetAsyncEnumerator(cancellationToken);
 
             try
             {
-                var set = new HashSet<TSource>(comparer);
-
                 while (await e.MoveNextAsync().ConfigureAwait(false))
                 {
                     set.Add(e.Current);
                 }
-
-                return set;
             }
             finally
             {
                 await e.DisposeAsync().ConfigureAwait(false);
             }
+
+            return set;
         }
     }
 }

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

@@ -22,23 +22,23 @@ namespace System.Linq
 
             async Task<List<TSource>> Core()
             {
+                var list = new List<TSource>();
+
                 var e = source.GetAsyncEnumerator(cancellationToken);
 
                 try
                 {
-                    var list = new List<TSource>();
-
                     while (await e.MoveNextAsync().ConfigureAwait(false))
                     {
                         list.Add(e.Current);
                     }
-
-                    return list;
                 }
                 finally
                 {
                     await e.DisposeAsync().ConfigureAwait(false);
                 }
+
+                return list;
             }
         }
     }