Reusing an IEnumerable<T> results in false result, e.g. on .Any()

156 views Asked by At

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:

.. It is basically an IEnumerable that allows me to iterate over the words within a given string.

1

There are 1 answers

1
Jon Skeet On BEST ANSWER

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.