Browse Source

Support ICollection for short-circuiting Count.

Bart De Smet 7 years ago
parent
commit
7a4bdf54bc
1 changed files with 8 additions and 7 deletions
  1. 8 7
      Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs

+ 8 - 7
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs

@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the Apache 2.0 License.
 // The .NET Foundation licenses this file to you under the Apache 2.0 License.
 // See the LICENSE file in the project root for more information. 
 // See the LICENSE file in the project root for more information. 
 
 
+using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Threading;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
@@ -68,14 +69,14 @@ namespace System.Linq
 
 
         private static Task<int> CountCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
         private static Task<int> CountCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
         {
         {
-            if (source is ICollection<TSource> collection)
+            switch (source)
             {
             {
-                return Task.FromResult(collection.Count);
-            }
-
-            if (source is IAsyncIListProvider<TSource> listProv)
-            {
-                return listProv.GetCountAsync(onlyIfCheap: false, cancellationToken);
+                case ICollection<TSource> collection:
+                    return Task.FromResult(collection.Count);
+                case IAsyncIListProvider<TSource> listProv:
+                    return listProv.GetCountAsync(onlyIfCheap: false, cancellationToken);
+                case ICollection collection:
+                    return Task.FromResult(collection.Count);
             }
             }
 
 
             return Core();
             return Core();