Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Iterator vs enumerator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael C - 03 Sep 2007 05:35 GMT
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
Jon Skeet [C# MVP] - 03 Sep 2007 07:34 GMT
> 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
[quoted text clipped - 4 lines]
>
> 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.

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

Göran Andersson - 03 Sep 2007 13:06 GMT
> 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
[quoted text clipped - 7 lines]
> 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.

Signature

Göran Andersson
_____
http://www.guffa.com

Michael C - 04 Sep 2007 02:27 GMT
> 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

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
Göran Andersson - 04 Sep 2007 21:49 GMT
>> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.