> An Enumerator is an iterator.
>
[quoted text clipped - 3 lines]
> When you are talking about differences between an Iterator and an
> Enumerator, you are talking about differences in specific
> implementations of iterators. For this discussion to go anywhere, you have
> to specify what exact implementations you are talking about.
>> An Enumerator is an iterator.
>>
[quoted text clipped - 6 lines]
> I think I get it, what they call an iterator is just a shortcut to avoid
> creating your IEnumerator class.
Actually an enumerator is generally a struct, not a class. Typically it
uses 16 bytes of stack space, which definitely isn't much to whine about.
> Basically the article was wrong in saying
> it will save a lot of memory.
It might be correct for a specific implementation of an enumerator, but
not for enumerators in general.
>> implementations of iterators. For this discussion to go anywhere, you have
>> to specify what exact implementations you are talking about.
[quoted text clipped - 7 lines]
>
> The GetNumbersFor method was not provided.
If the GetNumbersFor method would create a collection, the foreach
statement will create an enumerator for that collection. The enumerator
for a collection of course requires that the collection is present in
memory, but that requirement is only for that specific implementation of
an enumerator, not all enumerators.

Signature
Göran Andersson
_____
http://www.guffa.com
Michael C - 05 Sep 2007 03:29 GMT
>> Basically the article was wrong in saying it will save a lot of memory.
>
> It might be correct for a specific implementation of an enumerator, but
> not for enumerators in general.
I see what you mean. In the article there is nothing to indicate that the
specific implementation loads a large number of objects and nothing at the
end of the article to indicate that they solved it either. But I understand
what they were trying to say. Here's the article:
http://msdn2.microsoft.com/en-au/vcsharp/bb264519.aspx
> If the GetNumbersFor method would create a collection, the foreach
> statement will create an enumerator for that collection. The enumerator
> for a collection of course requires that the collection is present in
> memory, but that requirement is only for that specific implementation of
> an enumerator, not all enumerators.
That's true but the code at the end of the article appears to do the same
thing.
Michael
Jon Skeet [C# MVP] - 05 Sep 2007 07:32 GMT
> > It might be correct for a specific implementation of an enumerator, but
> > not for enumerators in general.
[quoted text clipped - 5 lines]
>
> http://msdn2.microsoft.com/en-au/vcsharp/bb264519.aspx
The core of the article isn't comparing enumerators vs iterators - it's
comparing a collection where all the elements are in memory at a time
(such as List and Dictionary) with a more "streaming" approach where
you only load and process one item at a time.
> > If the GetNumbersFor method would create a collection, the foreach
> > statement will create an enumerator for that collection. The enumerator
[quoted text clipped - 4 lines]
> That's true but the code at the end of the article appears to do the same
> thing.
No. The code at the end of the article never creates a collection. What
exactly do you mean?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Michael C - 05 Sep 2007 08:30 GMT
> The core of the article isn't comparing enumerators vs iterators - it's
> comparing a collection where all the elements are in memory at a time
> (such as List and Dictionary) with a more "streaming" approach where
> you only load and process one item at a time.
The link to the article is titled "Custom Iterators" and claims to talk
about Custom Iterators and the yield statement.
>> That's true but the code at the end of the article appears to do the same
>> thing.
>
> No. The code at the end of the article never creates a collection. What
> exactly do you mean?
Both the code at the start and end of the article has the line
IEnumerable<PhoneBookEntry> newYorkNumbers =
PhoneBook.FindListFor("New York");
Isn't this creating a collection in both cases?
Michael
Jon Skeet [C# MVP] - 05 Sep 2007 08:39 GMT
> > The core of the article isn't comparing enumerators vs iterators - it's
> > comparing a collection where all the elements are in memory at a time
[quoted text clipped - 3 lines]
> The link to the article is titled "Custom Iterators" and claims to talk
> about Custom Iterators and the yield statement.
Absolutely. That's not the same as "Comparing iterators and
enumerators" though - it's *implementing* iterators, and comparing
iterators with collections which have been completely loaded before
you start iterating.
> >> That's true but the code at the end of the article appears to do the same
> >> thing.
[quoted text clipped - 8 lines]
>
> Isn't this creating a collection in both cases?
No. It's fetching an IEnumerable<PhoneBookEntry>. That doesn't
necessarily fetch all the data in one go. It could be using a
DataReader, for example.
In particular, the article states:
<quote>
For this discussion, let's assume you've changed
PhoneBook.FindListFor() to be an enumerator method as well.
</quote>
Now, that doesn't prohibit it from being loaded in memory to start
with - but it could be reading a file line by line, and only returning
the next line when the caller asks for it.
Jon