I'm a little lost in deferred execution land:
I declare an instance of an IEnumerable implementing class
var wordEnumerable = new WordEnumerable(_text);
Then I iterate over it (the first word is "Lorem")
foreach (var word in wordEnumerable)
Console.WriteLine(word);
.. which is written to the console.
Now right thereafter in code I do a
Console.WriteLine(wordEnumerable.Any(w => w == "Lorem"));
.. and get a False as output.
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.
My expectation 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?
What am I missing here?
Update:
- Link to IEnumerable implementation: https://gist.github.com/814352
- Link to IEnumerator implementation: https://gist.github.com/814354
- .. and finally the underlying text parser implementation: https://gist.github.com/814358
.. It is basically an IEnumerable that allows me to iterate over the words within a given string.
Your expectation is correct -
Any
will callGetEnumerator
again to get a freshIEnumerator<T>
. That should be fine if you've implementedIEnumerable<T>
correctly. My guess is that your implementation ofWordEnumerable
is incorrect. Please post the code :)What happens if you write:
? The other thing to check is that
WordEnumerable
implementsIEnumerable<string>
rather thanIEnumerable<object>
, otherwise your==
check will be a reference identity check.