Open topic with navigation
WNSupported in Synergy .NET on Windows
YIELD MRETURN expression
An expression that is evaluated and returned as a value to the enumerator object.
The YIELD statement indicates to the compiler that the current method is an iterator block. The two versions of the YIELD syntax perform different functions:
Although an iterator is written as a method, the compiler translates it into a nested class that keeps track of the position of the iterator as long as the FOREACH loop on the client code continues. When YIELD MRETURN is reached, the current location is stored. Execution begins at this location the next time the iterator is called.
An iterator method must have a return type of IEnumerable or IEnumerable<>. A class can have multiple iterators.
A YIELD MRETURN statement cannot appear in a CATCH block, a TRY block that contains a CATCH, or a FINALLY block.
The example below returns powers of 2 using the YIELD statement.
import System.Collections namespace ns1 public class PowersOf2 public static method Power, @IEnumerable number, int exponent, int proc data counter, int, 0 data result, int, 1 while (counter < exponent) begin incr counter result = result * number yield mreturn result end endmethod endclass endnamespace main proc data i, int foreach i in ns1.PowersOf2.Power(2,8) begin Console.Write(i) Console.Write(" ") end Console.WriteLine()