C # 2.0 specification (iterator) (1)

zhaozj2021-02-16  48

22 iterator

22.1 iterator block

The iterator block is a statement block of an ordered sequence of generated values. The iterator block is distinguished from a conventional representation by one or more Yield statements.

l Yield RetURN statement generates the next value of iteration.

l Yield Break statement indicates iteration.

Iterator blocks can be used as a method for Method-Body, an Operator-Body, and an Accessor-Body. The premise is the return type of the corresponding function member is an enumerator interface. One or an enumerable interface one of the enumerated interfaces.

Iterator blocks are not unique in C # syntax. They are restricted in several ways, and the main roles are in the semantics of the function member statement, but they are only a statement block in grammar.

When a function member is implemented using an iterator block, specifying any REF or OUT parameters for the formal parameter list will result in compile time errors.

The RETURN statement appears in the iterator block will result in compile time errors (but Yield Return statements are allowed).

In the iterator block contains unsafe context (§ 18.1) will result in compile time errors. Even when the iterator declares in an insecure context, iterator blocks are always defined as a security context.

22.1.1 enumerator interface

Enumerator interface is the system.collections.ienumerator interface and all instances of System.Collections.Generic.ienumerator . In this chapter, these interfaces will be referenced accordingly as Ienumerator and IEnumerator .

22.1.2 Accessible interface

Enumerable Interface is the system.collections.ienumerable interface and all instances of System.Collections.Generic.ienumerable . In this chapter, these interfaces will be referenced accordingly as Ienumerable and IEnumerable .

22.1.3Yield

The iterator block generates a sequence with all values ​​of the same type. The type of Yield Type is called iterator block.

l The Yield type of the iterator block is usually used to implement a function member that returns IENUMERATOR or IENUMERABLE is an object.

l The Yield type of the iterator block is usually used to implement a function member that returns IENUMERATOR or IEnumerable is T.

22.1.4 THIS Access

The THIS expression is classified as a value within the iterator block of the instance member of the class. The type of this value is class type, which can be used in this type, this value is a reference to the object when the member is called.

In the iterator block of the instance member of the structure, this expression is classified as a variable. The type of variable is the type of structure, which can be used in this structure. This variable represents a copy of the corresponding structure when a member is called. In the iterator block of the structural instance, the behavior of the THIS variable is like a value parameter of the structural type.

22.2 enumeration objects

When a function member of the returner interface type uses an iterator block implementation, the call function member does not immediately execute the code in the iterator block. Instead, the enumerator object will be created and returned. The object encapsulates the code specified in the iterator block, and when the MoveNext method of the enumerator object is called, the code in the iterator block is executed. The enumerator object has the following characteristics.

l It implements IEnumerator and IEnumerator , T is the Yield type (generating type) of the iterator block.

l It implements system.idisposable. l It is initialized using a copy of the real entry (if any), and the instance value will be passed to the function member.

l It has four potential states Before, Running, Suspended, and After, and it is initialized before the Before state.

Enumerator objects are typically an enumerator class instance generated by a compiler that encapsulates the code in the iterator language sentence block and implements an enumerator interface, but other implementations are also possible. If an enumerator class is generated by the compiler, this class will be embedded, in the class containing the function member, the class will have a private accessibility, and the class has a name that is reserved as the compiler. (2.4.2).

Enumerator objects can implement more interfaces specified here.

The following sections describe the exact behavior of the MoveNext, Current, and Dispose members implemented by the IEnumerable and IEnumerable interface, which is provided by an enumeration object.

Note that the enumerator object does not support the Ienumerator.Reset method. Calling this method will throw System.NotsupportedException.

22.2.1MoveNext method

The MoveNext method of the enumerator object encapsulates the code of the iterator block. Calling the MoveNext method will execute the code within the iterator and set the Current property of the enumerated object to the appropriate value. Accurate action performed by the MoveNext method depends on the status of the enumerator object when the MOVENEXT method is called.

l If the enumerator object status is Before, call MoveNext

n will change the state to Running.

N will initialize the parameters of the iterator block (including this) to the real gate value and instance value saved when the enumerator object is initialized.

n From the start execution iterator block until the interrupt is performed (as described below).

l If the status of the enumerator object is running, the result of calling MoveNext is not specified.

l If the status of the enumerator object is SUSPENDED, call MoveNext

n will change the state to Running.

l Restoring all local variables and parameters (including this) value performs the value of the actuator last time Suspended. Note that the content of any object referenced by these variables may change because of the call of MoveNext before.

N restart executing the iterator block after being initiated after the start-up Yield Return statement, and this state will continue until the execution is interrupted (as described below).

l If the status of the enumerator object is an after, then call MoveNext will return false.

When Movenext executes iterator block, there are four ways to interrupt: through a Yield Return statement, through a Yield Break statement, reach the end point of the iterator block, and an exception is thrown, and is propagated to iterator block Outside.

l When you encounter a Yield Return statement (22.4), the following conditions will occur

n The given expression will be calculated, implicitly converted to the generating type (Yield Type), and is assigned to the Current property of the enumerated object.

The execution of the N iterative device will be suspended. All local variables and parameters (including this) are saved, and the location 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. The status of the N enumerator object is changed to SUSPENDED.

n MoveNext method returns TRUE to the caller, indicating that the iterator successfully advanced to the next value.

l When you encounter Yield Break statements, the following conditions will happen.

n If the Yield Break statement is within one or more TRY blocks, the Finally statement associated with it will be executed.

The status of the N enumerator object is changed to the After.

n MoveNext method returns false for the caller, indicating that iterations have been completed.

l When you encounter the end point of the iterator block, the following conditions will occur.

The status of the N enumerator object is changed to the After.

n MoveNext method returns false for the caller, indicating that iterations have been completed.

l When an exception is thrown and propagated outside the iterator block, the following conditions will occur.

N will perform a suitable Finally block due to exception propagation within the iterative block.

The status of the N enumerator object is changed to the After.

n For the calling party of the MoveNext method, exception spread will continue.

22.2.2 Current attribute

The current attribute 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 the current is the value set when the last time call MOVENEXT. When the enumerator object is in the Before, Running or AFTER state, the resulting result of accessing the Current is not specified.

For a Yield type iterator block with a non-Object type, the IENUMERABLE implementation of the enumerator object is implemented, corresponding to the IENUMERATOR access to the IEnumerator of the enumerator object, and convert the result to the Object Type .

22.2.3 Dispose method

The Dispose method cleanss the iterative results by setting the state of the enumerator object to After to clean up iteration.

l If the status of the enumerator object is Before, calling Dispose will change its status as After.

l If the status of the enumerator object is Running, the result of calling the Dispose is specified.

l If the status of the enumerator object is SUSPENDED, call Dispose

n Change its status as Running.

n Execute the finally block, as if the last executed Yield Return statement is a Yield Break statement. If an exception is thrown and propagated into the iterative device, the status of the enumerator object will be set to the AFTER, and the exception will be propagated to the caller of the Dispose method.

n Change its status as After.

l If the status of the enumerator object is After, call Dispose has no effect.

22.3 Accessible object

When a function member that returns an enumerable interface type uses an iterator block implementation, the call function member does not immediately execute the iterator block code. Instead, an enumerable object will be created and returned. The getENUMERATOR method that can enumerate the object returns an enumerator object, which encapsulates the code specified in the iterator block. When the MoveNext method of the enumerator object is called, the execution of the iterator block code will be triggered. Enumerable objects have the following characteristics. l It implements the IEnumerable and IEnumerable interface, where T is the type of generator block (Yield Type).

l It uses real-gate copies to initialize (if any), and passes the instance value to the function member.

A object is usually an enumerable instance generated by the compiler, which encapsulates the code of the iterator block and implements an enumerated interface, but other implementation methods are also possible. If an enumeration class is generated by a compiler, the class will embed in the class containing the function member and has a private accessibility, and a name that is used as the compiler (2.4.2).

A object can be implemented than more interfaces described herein. In particular, an enumerated object can also realize the IEnumerator and IEnumerator interface, which makes it either as an enumerable object as an enumerator object. In that type of implementation, the first call of the getENUMERATOR method that can be enumerated object will return to the object itself. For subsequent calls for the getENumerator method of this enumerable object, if any, a copy of the enumerated object will be returned. Therefore, each returned enumerator will have its own state, and the change in an enumerator does not affect another enumerator.

22.3.1 GetENUMERATOR Method

An enumerated object provides an implementation of the GetEnumerator method for the IEnumerable and IEnumerable interface. These two GetENUMERATOR methods share a public implementation, which is used to get and return a valid enumerator object.

The enumerator object is initialized using an actual gate value. When an object is enumerated, the exemplary value will be saved, on the other hand, the enumerator object function will be described in §22.2.

(To be commitUed)

转载请注明原文地址:https://www.9cbs.com/read-25893.html

New Post(0)