Iterators

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

Hi All,

Please could someone explain to me what an iterator is and
how it may be used in a real world situation.

I am new to C#, and I can't find a decent,plain english
explanation for iterators.

TIA
Kevin
 
Hi Kevin,

In general, an iterator is a means for sequential access to a container. A
container, in turn, is a data structure holding one or more data items.
Arrays, collections, hashtables and even trees are all containers. Iterators
encapsulate all logic necessary for sequential access to the container
elements.

For example, an iterator for an array keeps an index of a current element
and increments the index by one each time next element is requested.

As far as I know, iterators are introduced in C# 2.0 which has not been
released yet. They are implemented as co-routines (that is, routines that
run on a different fiber within the thread and therefore have own stack and
CPU context). Thus, if such an iterator encapsulates quite a complex logic
(say, an iterator that recursively walks a binary tree), it won't be using
the caller's stack for recursion.

P.S. Only one fiber can be active within a thread, as far as I know,
therefore iterators do not run IN PARALLEL with the calling code. Gurus
please correct me if I am wrong here.
 
They are not implemented as fibers. It's just syntactical shorthand for the
enumerator pattern.
ILDASM is your friend ;)
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com


Dmitriy Lapshin said:
Hi Kevin,

In general, an iterator is a means for sequential access to a container. A
container, in turn, is a data structure holding one or more data items.
Arrays, collections, hashtables and even trees are all containers. Iterators
encapsulate all logic necessary for sequential access to the container
elements.

For example, an iterator for an array keeps an index of a current element
and increments the index by one each time next element is requested.

As far as I know, iterators are introduced in C# 2.0 which has not been
released yet. They are implemented as co-routines (that is, routines that
run on a different fiber within the thread and therefore have own stack and
CPU context). Thus, if such an iterator encapsulates quite a complex logic
(say, an iterator that recursively walks a binary tree), it won't be using
the caller's stack for recursion.

P.S. Only one fiber can be active within a thread, as far as I know,
therefore iterators do not run IN PARALLEL with the calling code. Gurus
please correct me if I am wrong here.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Kevin said:
Hi All,

Please could someone explain to me what an iterator is and
how it may be used in a real world situation.

I am new to C#, and I can't find a decent,plain english
explanation for iterators.

TIA
Kevin
 
Dmitriy Lapshin said:
Hi Kevin,

In general, an iterator is a means for sequential access to a container. A
container, in turn, is a data structure holding one or more data items.
Arrays, collections, hashtables and even trees are all containers. Iterators
encapsulate all logic necessary for sequential access to the container
elements.

For example, an iterator for an array keeps an index of a current element
and increments the index by one each time next element is requested.

As far as I know, iterators are introduced in C# 2.0 which has not been
released yet. They are implemented as co-routines (that is, routines that
run on a different fiber within the thread and therefore have own stack and
CPU context). Thus, if such an iterator encapsulates quite a complex logic
(say, an iterator that recursively walks a binary tree), it won't be using
the caller's stack for recursion.

P.S. Only one fiber can be active within a thread, as far as I know,
therefore iterators do not run IN PARALLEL with the calling code. Gurus
please correct me if I am wrong here.

so what is the point of parceling the work of the iterator out to the
fiber? Just to limit the activity on the stack? Doesnt that
complicate exception handling? that is, if the iterator code throws
an exception, will the exception be ignored by the code outside of the
fiber ( the code that owns the iterator )

-Steve
 
so what is the point of parceling the work of the iterator out to the
fiber? Just to limit the activity on the stack? Doesnt that
complicate exception handling? that is, if the iterator code throws
an exception, will the exception be ignored by the code outside of the
fiber ( the code that owns the iterator )

They are not implemented in fibers. There is no need to implement a fiber.
Iterators are implemented with a simple enumerator pattern so that existing
languages can still interop against them. And even if the C# and CLR team
wanted to multi-thread this due sudden dementia, I'm sure they wouldn't use
fibers to do it. Fibers are only useful in a very narrow set of
circumstances that justify the micro perf gain and increased complexity.
 
Gents,

There is an article in MSDN magazine which shows how to do that with fibers
and which explains why it would be beneficial. The article is titled
"Implementing co-routines for .NET by Wrapping the Umnanaged Fiber API", the
author is Ajai Shankar. The article is published in September 2003 - Vol 18
No 9 issue of MSDN magazine, pp. 89-94
 
Okay, but the article doesn't have anything whatsoever to do with the
iterators implementation in C# - more to the point, I have ILDASM on my hard
drive and it says that fibers aren't being used. I also went to several
panel discussions at the PDC and my understanding of the future directions
in multi-threading support is different than the discussion in the article -
which is essentially just MC++ hackery that frankly can't be relied upon to
work (read the disclaimers at the end of the article), can't be made to work
with the current runtime (due to core architectural issues), and will
probably be completely broken in future versions of the runtime.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com



Dmitriy Lapshin said:
Gents,

There is an article in MSDN magazine which shows how to do that with fibers
and which explains why it would be beneficial. The article is titled
"Implementing co-routines for .NET by Wrapping the Umnanaged Fiber API", the
author is Ajai Shankar. The article is published in September 2003 - Vol 18
No 9 issue of MSDN magazine, pp. 89-94

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mickey Williams said:
They are not implemented in fibers. There is no need to implement a fiber.
Iterators are implemented with a simple enumerator pattern so that existing
languages can still interop against them. And even if the C# and CLR team
wanted to multi-thread this due sudden dementia, I'm sure they wouldn't use
fibers to do it. Fibers are only useful in a very narrow set of
circumstances that justify the micro perf gain and increased complexity.
 
Back
Top