Note that there are some explanatory texts on larger screens.

plurals
  1. POReusing an IEnumerable<T> results in false result, e.g. on .Any()
    text
    copied!<p>I'm a little lost in deferred execution land:</p> <p>I declare an instance of an IEnumerable implementing class</p> <pre><code>var wordEnumerable = new WordEnumerable(_text); </code></pre> <p>Then I iterate over it (the first word is "Lorem")</p> <pre><code>foreach (var word in wordEnumerable) Console.WriteLine(word); </code></pre> <p>.. which is written to the console.</p> <p>Now right thereafter in code I do a</p> <pre><code>Console.WriteLine(wordEnumerable.Any(w =&gt; w == "Lorem")); </code></pre> <p>.. and get a False as output.</p> <p>Now If I put the .Any(..) part above the foreach loop I do get a true, however the loop does start with the second word.</p> <p>My <em>expectation</em> was that .Net creates different runtime 'contexts' for each call to an IEnumerable and its underlying IEnumerator so they don't interfere... I wouldn't want to .Reset() it by hand in order to get a proper result?</p> <p>What am I missing here?</p> <p><strong>Update</strong>:</p> <ul> <li>Link to IEnumerable implementation: <a href="https://gist.github.com/814352" rel="nofollow">https://gist.github.com/814352</a></li> <li>Link to IEnumerator implementation: <a href="https://gist.github.com/814354" rel="nofollow">https://gist.github.com/814354</a></li> <li>.. and finally the underlying text parser implementation: <a href="https://gist.github.com/814358" rel="nofollow">https://gist.github.com/814358</a></li> </ul> <p>.. It is basically an IEnumerable that allows me to iterate over the words within a given string.</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