C # 2.0 specification (iterator) (i)

Source: Internet
Author: User
Tags finally block

22 iterators

22.1 iterator block

An iterator block is a statement block that produces an ordered sequence of values. An iterator block is distinguished from a regular statement block by one or more yield statements.
L yield return statement produces the next value of the iteration.
The L yield break statement specifies that the iteration is complete.
An iterator block can be used as a method body (method-body), an operation Fu (Operator-body), an accessor body (accessor-body), provided that the return type of the corresponding function member is one of the enumerator (enumerator) interfaces or is enumerable ( Enumerable) One of the interfaces.
The iterator block does not have any unique elements in the C # syntax. They are limited in several ways, and the primary role is in the semantics of function member declarations, but they are syntactically only a block of statements.
When a function member is implemented with an iterator block, specifying any ref or out parameter for the formal parameter list causes a compile-time error.
A return statement that appears in the iterator block causes a compile-time error (but the yield return statement is allowed).
Including an unsafe context (§18.1) in an iterator block will result in a compile-time error. Even when an iterator declaration is embedded in an unsafe context, the iterator block is always defined as a security context.

22.1.1 Enumerator interface

The enumerator interface (enumerator interface) [/b] is the System.Collections.IEnumerator interface and system.collections.generic.ienumerator<t All instances of >. In this chapter, these interfaces are referred to as IEnumerator and ienumerator<t> accordingly.

22.1.2 Enumerable interface

An enumerable interface (enumerable interface[/b]) [/b] is System.Collections.IEnumerable interface and system.collections.generic.ienumerable< All instances of t>. In this chapter, these interfaces are referred to as IEnumerable and ienumerable<t> accordingly.

22.1.3Yield type

An iterator block generates a sequence of all values of the same type. Yield[/b] Type (yield type[/b]) [/b] for the type is called the iterator block.
The yield type of the L iterator block is typically used to implement a function member that returns IEnumerator or IEnumerable is an object.
The yield type of the L iterator block is typically used to implement a function member that returns ienumerator<t> or ienumerable<t> is T.

22.1.4 this access

Within an iterator block of an instance member of a class, the this expression is categorized as a value. The type of the value is the class type, which can be used in this type, which is a reference to the object when the member is called.
Within an iterator block of an instance member of a struct, the this expression is categorized as a variable. The type of the variable is the struct type, which it can use in this structure. The variable represents a copy of the corresponding structure when a member is called. Within an iterator block of a struct instance member, the this variable behaves as if it were a value parameter of the struct type.

22.2 Enumerating Objects

When a function member that returns an enumerator interface type uses an iterator block implementation, calling a function member does not immediately execute the code in the iterator block. Instead, the enumerator objects (enumerator object) are created and returned. The object encapsulates the code specified in the iterator block, and the code in the iterator block executes when the MoveNext method of the enumerator object is called. The enumerator object has the following characteristics.
L It implements IEnumerator and ienumerator<t>,t as the yield type of the iterator block (the resulting type).
L it implements the System.IDisposable.



L It is initialized with a copy of the argument value (if any), and the instance value is passed to the function member.
L It has four potential states before, running, suspended, and after, and it is initialized before the before state. The

Enumerator object is typically a compiler-generated enumerator class instance that encapsulates the code in the iterator statement block and implements the enumerator interface, but the other implementation methods are also possible. If an enumerator class is generated by the compiler, the class will be embedded, and in the class containing the function member, the class will have private accessibility, and the class has a name (§2.4.2) reserved for the compiler. The
Enumerator object can implement more interfaces than specified here. The
subsequent sections describe the exact behavior of MoveNext, current, and dispose members implemented by the IEnumerable and Ienumerable<t> interfaces, which are provided by the enumeration object.
Note that the enumerator object does not support the Ienumerator.reset method. Calling this method throws a System.NotSupportedException exception.

22.2.1MoveNext method

The MoveNext method of the enumerator object encapsulates the code of the iterator block. Calling the MoveNext method executes the code within the iterator and sets the current property of the enumeration object to the appropriate value. The exact action performed by the MoveNext method depends on the state of the enumerator object when the MoveNext method is invoked.
L If the enumerator object state is before, call MoveNext
N will change the status to running.
N initializes the parameters of the iterator block (including this) to the argument values and instance values that are persisted when the enumerator object is initialized.
N Executes the iterator block from the beginning until execution is interrupted (as described below).
L If the state of the enumerator object is running, the result of calling MoveNext is unspecified.
L If the state of the enumerator object is suspended, call MoveNext
N will change the status to running.


L restores the value of all local variables and parameters (including this) to the execution state when the last time the iterator was suspended (suspended). Note that the contents of any object referenced by these variables may change because of the previous call to MoveNext.
n resumes execution of the iterator block after the execution of a pending yield return statement, and the state continues until execution is interrupted (described below).
L If the state of the enumerator object is after, then calling MoveNext returns False.
When MoveNext executes an iterator block, there are four ways to break execution: through a yield break statement, the end point of the iterator block is reached, and an exception is thrown and propagated beyond the iterator block.
L When a yield return statement is encountered (§22.4), the following happens
n the expression given in the statement is evaluated, implicitly converted to the resulting type (yield type), and assigned to the current property of the enumeration object.
Execution of the N iterator body will be suspended. The values and parameters of all local variables (including this) are saved, and the position of the yield return statement is also saved. If the yield return statement is within one or more try blocks, the finally block associated with it will not be executed at this time.
n the state of the enumerator object is changed to suspended.
The N MoveNext method returns true to the caller, indicating that the iterator has successfully advanced to the next value.
L When a yield break statement is encountered, the following happens
n If the yield break statement is within one or more try blocks, the finally statement associated with it will be executed.
n the state of the enumerator object is changed to after.
The N MoveNext method returns false to the caller, indicating that the iteration has completed.
When the end point of an iterator block is encountered, the following happens.
n the state of the enumerator object is changed to after.
The N MoveNext method returns false to the caller, indicating that the iteration has completed.


When an exception is thrown and propagated outside of the iterator block, the following occurs.
n within the iterator block, the appropriate finally block will be executed due to the exception propagation (exception propagation).
n the state of the enumerator object is changed to after.
N for a call to the MoveNext method Towners said, the exception propagation will continue.

22.2.2 Current Property

The current property of the enumerator object is affected by the yield return statement of the iterator block.
When the enumerator object is in the suspended state, the value of current is the value set at the last call to MoveNext. When the enumerator object is in the before, running, or after state, the resulting result of accessing current is unspecified.
For a yield-type iterator block with a non-object type, the IEnumerable implementation of the enumerator object accesses the current implementation, which corresponds to the implementation of the current gain through the ienumerator<t> of the enumerator object, and converts the result to the object type.

22.2.3 Dispose method

The Dispose method cleans up the iteration results by placing the state of the enumerator object as after.
If the state of the enumerator object is before, calling Dispose will change its state to after.
L If the state of the enumerator object is running, the result of calling Dispose is specified.
L If the state of the enumerator object is suspended, calling Dispose will
N change its state to running.
N executes a finally block, as if the last yield return statement is a yield break statement. If an exception is thrown and propagated outside the iterator body, the state of the enumerator object is set to after, and the exception is propagated to the caller of the Dispose method.
n change its status to after.
If the state of the enumerator object is after, calling Dispose has no effect.

22.3 Enumerable objects

When a function member that returns an enumerable interface type is implemented using an iterator block, the calling function member does not immediately execute the iterator block code. Instead, an enumerable object ([/b]enumerable object[/b]) [/b] is created and returned. The GetEnumerator method of an enumerable object returns an enumerator object that encapsulates the code specified in the iterator block, triggering the execution of the iterator block code when the MoveNext method of the enumerator object is called. An enumerable object has the following characteristics.
L It implements the IEnumerable and Ienumerable<t> interfaces, where T is the generation type (yield type) of the iterator block.
L It initializes (if any) with a copy of the argument value and passes the instance value to the function member. The

object is typically an instance of a compiler-generated enumerable class that encapsulates the code of an iterator block and implements an enumerable interface, but other implementations are also possible. If an enumerable class is generated by the compiler, the class is embedded in the class that contains the function member and has private accessibility, and a name (§2.4.2) that is reserved for use by the compiler. The
can object can implement more interfaces than described here. In particular, enumerable objects can also implement the IEnumerator and Ienumerator<t> interfaces, which makes them both an enumerable object and an enumerator object. In that implementation type, the first call to the GetEnumerator method of an enumerable object returns the enumerable object itself. Subsequent calls to the GetEnumerator method of the enumerable object, if any, will return a copy of the enumerable object. Therefore, each returned enumerator will have its own state, and the changes made in one enumerator will not affect the other enumerator.

22.3.1 GetEnumerator Method

An enumerable object provides an implementation of the GetEnumerator method for the IEnumerable and Ienumerable<t> interfaces. These two GetEnumerator methods share a common implementation, which is used to get and return a valid enumerator object.
The enumerator object is initialized with an argument value, and its instance value is saved when an enumerable object is initialized, and the enumerator object function is described as §22.2.
(To be continued)

The above is the C # 2.0 specification (iterator) (a) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.