Browse Source

Merge pull request #795 from quinmars/hide-enumerable

Hide List<E> with .Skip(0)
Daniel C. Weber 7 years ago
parent
commit
3366a19f36

+ 6 - 7
Rx.NET/Source/src/System.Reactive/Internal/Lookup.cs

@@ -46,13 +46,12 @@ namespace System.Reactive
             }
             }
         }
         }
 
 
-        private IEnumerable<E> Hide(List<E> elements)
-        {
-            foreach (var x in elements)
-            {
-                yield return x;
-            }
-        }
+        // Instead of yielding the elements in a foreach loop, zero
+        // elements are skipped. Technically the result is the same
+        // with the benefit that the LINQ implementation can internally
+        // generate a type that keeps track of count of the enumerable.
+        // Consecutive operators can benefit from that knowledge.
+        private static IEnumerable<E> Hide(List<E> elements) => elements.Skip(0);
 
 
         public IEnumerator<IGrouping<K, E>> GetEnumerator()
         public IEnumerator<IGrouping<K, E>> GetEnumerator()
         {
         {

+ 8 - 0
Rx.NET/Source/tests/Tests.System.Reactive/Tests/Linq/Observable/ToLookupTest.cs

@@ -199,6 +199,14 @@ namespace ReactiveTests.Tests
             Assert.False(d1.Contains("2"));
             Assert.False(d1.Contains("2"));
         }
         }
 
 
+        [Fact]
+        public void ToLookup_Hides_Internal_List()
+        {
+            var d1 = Observable.Range(1, 10).ToLookup(x => (x % 2).ToString()).First();
+            Assert.False(d1["0"] is ICollection<int>);
+            Assert.False(d1["0"] is IList<int>);
+        }
+
         [Fact]
         [Fact]
         public void ToLookup_Groups()
         public void ToLookup_Groups()
         {
         {