Show/Hide Toolbars

XSharp

Purpose

When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator.
 

Syntax

YIELD RETURN <expression>
YIELD (EXIT|BREAK)

Arguments

YIELD RETURNYou use YIELD RETURN <expression> to return each element one at a time.

 

YIELD [BREAK|EXIT]You use YIELD BREAK or YIELD EXIT to end the iteration.

Examples

using System.Collections.Generic
 
// The Yield return statement allows you to create code that returns a
// collection of values without having to create the collection in memory first.
// The compiler will create code that "remembers" where you were inside the
// loop and returns to that spot.
FUNCTION Start AS VOID
  FOREACH nYear AS INT IN GetAllLeapYears(1896, 2040)
     ? "Year", nYear, "is a leap year."
  NEXT
  Console.ReadLine()
RETURN
 
FUNCTION GetAllLeapYears(nMin AS INT, nMax AS INT) AS IEnumerable<INT>
  FOR LOCAL nYear := nMin AS INT UPTO nMax
    IF nYear % 4 == 0 .and. (nYear % 100 != 0 .or. nYear % 400 == 0)
        YIELD RETURN nYear
    END IF
    IF nYear == 2012
        YIELD EXIT     // Exit the loop
    ENDIF
  NEXT