473,946 Members | 4,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7494
Michael C <mi**@nospam.co mwrote:
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.co m>
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.co mwrote in message
news:Oj******** ******@TK2MSFTN GP05.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.GetNu mbersFor("New York"))
{
}

The GetNumbersFor method was not provided.

Michael
Sep 4 '07 #4
Michael C wrote:
"Göran Andersson" <gu***@guffa.co mwrote in message
news:Oj******** ******@TK2MSFTN GP05.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.
>implementation s 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.GetNu mbersFor("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.co mwrote in message
news:O%******** ********@TK2MSF TNGP02.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.co mwrote:
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.co m>
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
1513
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. Lovely. My memory cannot dredge up how to use an enumerator. I do not understand the documentation, whcih does not contain little code snippets of examples like MSDN Oct 2001 for older technologies did. Can someone give me an enumerator basics 101...
18
2207
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
9
7763
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 other day and stopped to think for a minute about whether it was right or not. foreach (XmlNode node in doc.SelectNodes("/root/map/");) { //Do something }
14
4911
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 to an iterator method (one returning IEnumerable). I WOULD like to do this for transformative operations on a collection. I realize the need to lock target, etc. Does anyone know how to handle 'writable iterators' in C# 2?
6
2814
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 as an experiment. The old version had the usual IEnumerator stuff and, in Current, returned:
2
4248
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 IEnumerable, ie public IEnumerable AnotherSortOrder() { yield x;....} Any insights out there as to why the additional iteration methods did not just return IEnumerator? (its just a little confusing, hoping for some better insight)
9
2315
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
3648
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 from object to string frequently). Basically generics and not its non- generic counterpart. string str1 = "abc: value1 def: value2 ghi: value3"; char delimiterChars = { '\t' }; string tokens = str1.Split(delimiterChars);
5
2121
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 FilterType { None, ADX MOM
0
9980
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11556
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9884
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8247
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7412
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6111
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3538
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.