Yield Statements

The yield statement is used when generating a sequence to add a new value into the generated sequence. It can be used in two places: in iterator methods and for loop expressions.

Please refer to these two topics for more details:

Examples:

Using yield in an iterator, the following method will return a sequence with 12 items: start, Value 0, Value 1, Value 2, Value 3, Value 4, Value 5, Value 6, Value 7, Value 8, Value 9, end:

type
  MyClass = public class
  public
    method Test: sequence of String; iterator;
  end;

Implementation:

method MyClass.Test: sequence of String;
begin
  yield 'start';
  for i : Integer := 0 to 9 do
    yield 'Value: '+i;
  yield 'end';
end;

...
for each val in myClassInstance.Test do begin
  Console.WriteLine(val);
end;

Using yield in a for loop expression, this creates a new sequence in x containing 10 strings with Value 0 through Value 9:

var x: sequence of string := for i := 0 to 0 yield 'Value: '+i;