List-Enumeration
An iterator is a read-only and forward cursor on a value sequence (SET. The iterator either implements the IEnumerator interface or the IEnumerator <T> interface.
From a technical point of view, if an object has the MoveNext method and the Current attribute, we can regard it as an iterator.
We can use the foreach statement to iterate an object that can be listed. An object that can be iterated is actually a logical embodiment of a sequence. An object that can be enumerated is not only a cursor, but also a cursor that can be generated to iterate itself. Therefore, the enumerated objects have two features:
- Implements the IEnumerator interface or the IEnumerator <T> Interface
- There is a method GetEnumerator, which returns an iterator
Enumeration mode:
IteratorVariableType Current {Enumeration pattern
To better understand the probability and pattern above, let's look at the two examples below
(CSample 1 (enumerator =Sample 2
Sample1 adopts an advanced method such as foreach to iterate strings (because the string class implements CharEnumerator), while Sample2 uses the underlying method to complete the iteration of strings. For the Sample, we use the using statement because CharEnumerator implements the IDisposable interface. The following code shows most of the CharEnumrator code (from Microsoft official website)
. Str =. index =-(index <(str. Length-++ = (str! ==== () =-CharEnumerator initialization set
We can use a single statement instance to list objects. For example: IList <Int> list = new List <int> {1, 2, 3}; during compilation, the compiler automatically translates it:
List <> );Translated Code
This is because the enumerated object implements the IEnumerable interface and also contains the Add method.
To verify this point, we can check the IL code to confirm:
[Mscorlib] System. Collections. Generic. list' <[mscorlib] System. Collections. Generic. list' <> ::(! [Mscorlib] System. Collections. Generic. list' <> ::(! [Mscorlib] System. Collections. Generic. list' <> ::(! [Mscorlib] System. Console: ReadLine ()IL Code Iterator-Iterator
Since foreach can be applied to the list, an enumerate can generate an iterator. It's confusing. Let's take a look at the following example: Use the iterator to return the Fibonacci series.
IEnumerable <> fig (I =, prevFib =, curFib =; I <number; I ++ newFib = prevFib + = Main (f Fibonacci (Fibonacci
Note that yield return is used in the above Code. So what is the difference between it and return?
Return: returns a value from the method.
Yield return: generates the next element from the current iterator. Each time the yield statement is executed, the control of the program is returned to the caller, and the status of the caller is retained. This allows the method to continue executing when the caller lists the next element. The lifecycle of the status of the caller depends on the list. Because of this, the status of the caller is released after the list is completed.
Iterator syntax
An iterator can be a method, attribute, or cited by containing one or more yield statements. The iterator must return one of the following four types: IEnumerable, IEnumerable <T>, IEnumerator, and IEnumerator <T>
Before proceeding to the next step, let's take a look at the definition of the IEnumerable interface and IEnumerator.
IEnumerator & IEnumerable
The iterator has a different syntax from the list, that is, the iterator needs to return the interface that can be listed or the list interface.
Create Sequence
The iterator can be further used to create iterations. To prove this, we can extend our Fibonacci series example.
IEnumerable <> maid (I =, prevFib =, curFib =; I <number; I ++ newFib = prevFib + = IEnumerable <> EvenNumbers (IEnumerable <> (x % = Main (f EvenNumbers (Fibonacci (Composable Iterator
Note that when the MoveNext () method of the series generated by the Fibonacci method is called (executing the foreach loop will implicitly call the MoveNext method of IEnumerator ), to determine whether the element is an even number.
The iterator can be further used to create iterations that are widely used in LINQ.