Reusing an IEnumerable<T> results in false result, e.g. on .Any()
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, how开发者_StackOverflowever 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 call GetEnumerator
again to get a fresh IEnumerator<T>
. That should be fine if you've implemented IEnumerable<T>
correctly. My guess is that your implementation of WordEnumerable
is incorrect. Please post the code :)
What happens if you write:
Console.WriteLine("First");
foreach (var word in wordEnumerable)
{
Console.WriteLine(word);
}
Console.WriteLine("Second");
foreach (var word in wordEnumerable)
{
Console.WriteLine(word);
}
? The other thing to check is that WordEnumerable
implements IEnumerable<string>
rather than IEnumerable<object>
, otherwise your ==
check will be a reference identity check.
精彩评论