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