473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Iterator vs enumerator

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
Sep 3 '07 #1
6 7442
Michael C <mi**@nospam.comwrote:
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.

--
Jon Skeet - <sk***@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
Sep 3 '07 #2
Michael C wrote:
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
_____
http://www.guffa.com
Sep 3 '07 #3
"Göran Andersson" <gu***@guffa.comwrote in message
news:Oj**************@TK2MSFTNGP05.phx.gbl...
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
Sep 4 '07 #4
Michael C wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:Oj**************@TK2MSFTNGP05.phx.gbl...
>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.
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.

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
_____
http://www.guffa.com
Sep 4 '07 #5
"Göran Andersson" <gu***@guffa.comwrote in message
news:O%****************@TK2MSFTNGP02.phx.gbl...
>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
Sep 5 '07 #6
Michael C <mi**@nospam.comwrote:
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
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
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.
No. The code at the end of the article never creates a collection. What
exactly do you mean?

--
Jon Skeet - <sk***@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
Sep 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephanie Stowe | last post by:
Hi. I am trying to understand the weird System.DirectoryServices object model. I have a DirectoryEntry object. I want to enumerate through the PropertyCollection. So I looked at GetEnumerator....
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
9
by: Anthony Bouch | last post by:
Everything I know about looping structures says to be careful about expressions that need to be evaluated again and again for each test/increment of a loop. I came across this piece of code the...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
6
by: emma_middlebrook | last post by:
Hi I know there's a SortedDictionary in C# 2.0 but I was just converting my existing implementation of SortedHashtable (which just wraps a Hashtable and provides an enumerator) to use iterators...
2
by: =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post by:
When creating multiple iterators, the original is defined as returning IEnumerator, ie public IEnumerator GetEnumerator() { yield x; ...} whereas the additional ones are defined as returning...
9
by: Larry Bates | last post by:
Does anyone know if there is a way to make a Python COM object act like a proper iterator in VB/Delphi? Example: Python COM object class foo: _public_methods_=
3
by: Dave | last post by:
I'm calling string.Split() producing output string. I need direct access to its enumerator, but would greatly prefer an enumerator strings and not object types (as my parsing is unsafe casting...
5
by: =?Utf-8?B?YjF1Y2VyZWU=?= | last post by:
hi, i need help building multiple instances of an enumerator and naming each one filter1, filter2, filterN depending on how many the user requires, here is the code i have so far public enum...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.