Iterator vs enumerator

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

I'm reading about Iterators and the article I'm reading states that an
enumerator must load all of the objects into memory, which is obviously a
big waste if there are a large number of objects and you only need a few of
them. On the other hand the iterator does not need to load more than 1
object hence uses less memory. But can't you do the same thing with an
enumerator just by returning the next object when Current is called?

If that's the case what's the advantage of an iterator?

Thanks,
Michael
 
Michael C said:
I'm reading about Iterators and the article I'm reading states that an
enumerator must load all of the objects into memory, which is obviously a
big waste if there are a large number of objects and you only need a few of
them. On the other hand the iterator does not need to load more than 1
object hence uses less memory. But can't you do the same thing with an
enumerator just by returning the next object when Current is called?

If that's the case what's the advantage of an iterator?

What is it describing as the difference between "enumerator" and
"iterator"? I regard them as equivalent terms - I prefer "iterator"
because it avoids confusion with an enumeration.
 
Michael said:
I'm reading about Iterators and the article I'm reading states that an
enumerator must load all of the objects into memory, which is obviously a
big waste if there are a large number of objects and you only need a few of
them. On the other hand the iterator does not need to load more than 1
object hence uses less memory. But can't you do the same thing with an
enumerator just by returning the next object when Current is called?

If that's the case what's the advantage of an iterator?

Thanks,
Michael

An Enumerator is an iterator.

Here, the C# example of an iterator uses an Enumerator:
http://en.wikipedia.org/wiki/Iterator

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.
 
Göran Andersson said:
An Enumerator is an iterator.

Here, the C# example of an iterator uses an Enumerator:
http://en.wikipedia.org/wiki/Iterator

When you are talking about differences between an Iterator and an
Enumerator, you are talking about differences in specific

I think I get it, what they call an iterator is just a shortcut to avoid
creating your IEnumerator class. Basically the article was wrong in saying
it will save a lot of memory.
implementations of iterators. For this discussion to go anywhere, you have
to specify what exact implementations you are talking about.

The article didn't specify an particular implementation, it was just a fake
class with no code given, something like this:

foreach(Entry e in PhoneBook.GetNumbersFor("New York"))
{
}

The GetNumbersFor method was not provided.

Michael
 
Michael said:
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.
The article didn't specify an particular implementation, it was just a fake
class with no code given, something like this:

foreach(Entry e in PhoneBook.GetNumbersFor("New York"))
{
}

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.
 
Göran Andersson said:
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
 
Michael C said:
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

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.
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?
 
Jon Skeet said:
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.
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
 
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.
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top