Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define IEnumerable behavior by contract?
    text
    copied!<p>Consider this 2 methods that returns IEnumerable:</p> <pre><code> private IEnumerable&lt;MyClass&gt; GetYieldResult(int qtResult) { for (int i = 0; i &lt; qtResult; i++) { count++; yield return new MyClass() { Id = i+1 }; } } private IEnumerable&lt;MyClass&gt; GetNonYieldResult(int qtResult) { var result = new List&lt;MyClass&gt;(); for (int i = 0; i &lt; qtResult; i++) { count++; result.Add(new MyClass() { Id = i + 1 }); } return result; } </code></pre> <p>This code shows 2 different behaviors when calling some method of IEnumerable:</p> <pre><code> [TestMethod] public void Test1() { count = 0; IEnumerable&lt;MyClass&gt; yieldResult = GetYieldResult(1); var firstGet = yieldResult.First(); var secondGet = yieldResult.First(); Assert.AreEqual(1, firstGet.Id); Assert.AreEqual(1, secondGet.Id); Assert.AreEqual(2, count);//calling "First()" 2 times, yieldResult is created 2 times Assert.AreNotSame(firstGet, secondGet);//and created different instances of each list item } [TestMethod] public void Test2() { count = 0; IEnumerable&lt;MyClass&gt; yieldResult = GetNonYieldResult(1); var firstGet = yieldResult.First(); var secondGet = yieldResult.First(); Assert.AreEqual(1, firstGet.Id); Assert.AreEqual(1, secondGet.Id); Assert.AreEqual(1, count);//as expected, it creates only 1 result set Assert.AreSame(firstGet, secondGet);//and calling "First()" several times will always return same instance of MyClass } </code></pre> <p>It's simple to choose which behavior I want when my code returns IEnumerables, but how can I explicitly define that some method gets an IEnumerable as parameter that creates a single result set dispite of how many times it calls "First()" method.</p> <p>Of course, I don't want to force all itens to be created unnecessarily and I want to define the parameter as IEnumerable to say that no item will be included or removed from the collection.</p> <p>EDIT: Just to be clear, the question is not about how yield works or why IEnumerable can return different instances for each call. The question is how can I specify that a parameter should be a "search only" collection that returns same instances of MyClass when I call methods like "First()" or "Take(1)" several times.</p> <p>Any ideas?</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload