Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Of course, I don't want to force all itens to be created unnecessarily</p> </blockquote> <p>In which case you need to allow the method to create them on demand, and if objects are created on demand (and without some form of cache) they will be <em>different</em> objects (at least in the sense of being different references&mdash;the default definition of equality for non-value objects).</p> <p>If your objects are inherently unique (i.e. they don't define some value based equality) then each call to <code>new</code> will create a different object (whatever the constructor parameters).</p> <p>So the answer to</p> <blockquote> <p>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> </blockquote> <p>is "you can't" except by creating one set of objects and repeatedly returning the same set, <em>or</em> by defining equality to be something different.</p> <hr> <p>Additional (based on comments). If you really want to be able to replay (for want of a better term) the same set of objects without building the whole collection you could cache want has already been generated and replay that first. Something like:</p> <pre><code>private static List&lt;MyData&gt; cache = new List&lt;MyData&gt;(); public IEnumerable&lt;MyData&gt; GetData() { foreach (var d in cache) { yield return d; } var position = cache.Count; while (maxItens &lt; position) { MyData next = MakeNextItem(position); cache.Add(next); yield return next; } } </code></pre> <p>I expect it would be possible to build such a caching wrapper around an iterator as well (<code>while</code> would become <code>foreach</code> over underlying iterator, but you would need to cache that iterator or <code>Skip</code> to the require position if the caller iterated beyond the cahing <code>List</code>).</p> <p><strong>NB</strong> any caching approach would be hard to make thread safe.</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