1. First, let's take a look at the definition of the IEnumerable interface:
Namespace System. Collections
{
// Summary:
// Exposes the enumerator, which supports a simple iteration over a non-generic
// Collection.
[ComVisible (true)]
[Guid ("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
Public interface IEnumerable
{
// Summary:
// Returns an enumerator that iterates through a collection.
//
// Returns:
// An System. Collections. IEnumerator object that can be used to iterate through
// The collection.
[DispId (-4)]
IEnumerator GetEnumerator ();
}
}
This interface is the top priority, and the iterator can implement this interface for iteration.
2. You must also know the yield keyword of C #.
Yield is used in the iterator block to provide values to enumeration objects or send an iteration end signal. It has two forms:
Yield return <expression>;
Yield break;
Note:
Expression must be implicitly converted to the yield type of the iterator.
Yield statements can only appear in the iteration module, but not in insecure modules or anonymous methods. When used together with expressions, the yield return statement cannot appear in catch blocks or try blocks containing one or more catch clauses.
3. introduce the concept of iterator
In fact, the so-called iteration refers to a loop, and the iterator refers to a way to implement this loop.
The iterator is a function added by C #2.0. It can be a method, get accessor or operator. In the end, it enables you to support foreach iteration in the class or structure. You do not need to implement the entire IEnumerable interface in implementation. You only need to provide an iterator. When the compiler detects the iterator time, the Current, MoveNext, and Dispose methods of the IEnumerable or IEnumerable <T> interface are automatically generated.
An iterator is a piece of code that can return an ordered sequence of values of the same type. It uses the yield return statement to return each element at a time and uses yield break to terminate the iteration, the return type must be IEnumerable, IEnumerator, IEnumerable <T>, or IEnumerator <T>.
3. The most common method to create an iterator is to implement the GetEnumerator method in the IEnumerable interface. The IEnumerable interface is as follows:
Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ctochina.net
{
Class Program
{
Static void Main (string [] args)
{
MyIterator myIterator = new MyIterator ();
Foreach (int I in myIterator)
{
Console. WriteLine (I );
}
}
}
Public class MyIterator
{
Public IEnumerator GetEnumerator ()
{
For (int I = 1; I <8; I ++)
{
Yield return I;
}
}
}
}
Output:
1 2 3 4 5 6 7
Note System. Collections; namespace reference, because IEnumerator is in
The GetEnumerator method in the IEnumerable interface is implemented, so only one GetEnumerator iteration can be performed in a class.
4. Another method to implement IterationThe code is followed by the following example:
Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ctochina.net
{
Class Program
{
Static void Main (string [] args)
{
MyIterator myIterator = new MyIterator ();
// Iteration 1