3

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.

Jörg Battermann
  • 4,044
  • 5
  • 42
  • 79
  • 2
    How does `WordEnumerable` implement `IEnumerable`? – Daniel A. White Feb 07 '11 at 13:09
  • Could you provide us with more info about WordEnumerable class? I have no problems with creating functionality you want using simple `List`. And if collection was not changed between both code fragments, than your code should work as well, if WordEnumerable class works as expected. – Jarek Feb 07 '11 at 13:13
  • There we go, added links to sources – Jörg Battermann Feb 07 '11 at 13:33

1 Answers1

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194