The Iterator mode is also called the Cursor mode. It is defined as: provides a method to access each element in a container object without exposing the internal details of the object. The iterator mode is related to containers. The access to container objects is designed to traverse algorithms.
The iterator mode consists of the following roles:
- Iterator: The Iterator role defines interfaces for accessing and traversing elements.
- Concrete Iterator: The Iterator role must implement the Iterator interface and record the current position in the traversal.
- Container role: the Container role is responsible for providing interfaces for creating specific iterator roles.
- Concrete Container: the interface for creating a specific iterator role for a specific Container role-This iterator role is related to the structure of the Container.
Intent: provides a method to access a collection object without exposing the internal details of the object.
Iterator mode in. NET
The Iterator Mode Implemented in. NET already exists for the clustering interface and Iterator interface, and IEnumerator plays the role of Iterator. The Code is as follows:
Public interface IEnumerator
{
Object Current
{
Get;
}
Void Reset ();
Bool MoveNext ();
}
IEnumerable assumes the abstract aggregation role and has only one GetEnumertor () method. This interface must be implemented if the set object needs to have the iterative traversal function.
Public interface IEnumerable
{
IEnumerator GetEnumerator ();
}
Next, let's take a look at the Online Shopping. The PersonsEnumerator class is an enumerator class.
Public class Persons: IEnumerable
{
Public string [] m_Names;
Public Persons (string [] names)
{
M_Names = new string [names. Length];
Names. CopyTo (m_Names, 0 );
}
Private string this [int index]
{
Get
{
Return m_Names [index];
}
Set
{
M_Names [index] = value;
}
}
Public IEnumerator GetEnumerator ()
{
Return new PersonsEnumerator (this );
}
}
Public class PersonsEnumerator: IEnumerator
{
Private int index =-1;
Private Persons p;
Public PersonsEnumerator (Persons p)
{
This. p = p;
}
Public bool MoveNext ()
{
If (index <p. m_Names.Length)
{
Return true;
}
Return false;
}
Public void Reset ()
{
Index =-1;
}
Public object Current
{
Get
{
Return p. m_Names [index];
}
}
}
In. NET2.0 and later versions, the yield return keyword makes implementation easier:
Public class Persons: IEnumerable
{
Private string [] m_names;
Public Persons (string [] names)
{
M_names = new string [names. Length];
Names. CopyTo (m_names, 0 );
}
Public IEnumerator GetEnumerator ()
{
Foreach (string name in m_names)
{
Yield return name;
}
}
}
In addition, Let's briefly introduce the yield Keyword:
The keyword yield is used in the iterator block to provide a value to the enumerated number object or send an iteration end signal. The iterator has two special statements: yield return <expression>;
Yield break;
Iterator Block
An iterator block is a code base with one or more yield statements. Any of the following three types of code blocks can be iterator blocks:
Method subject
Accessors
Operator subject
The Yield statement can only appear in the iterator block. This block can be used as a method, operator, or accessors. In this type of method, operators or accessors are subject to the following constraints:
- Insecure blocks are not allowed.
- Method, operator, or accessor parameter cannot be ref or out
- Yield statements cannot appear in anonymous methods.
- The Yield return statement cannot exist in the try block containing one or more catch clauses in the Catch Block.
The falling block of the Yield statement can generate two types of objects: IEnumerator and IEnumerable: