// Return the items in reverse.
if (ReturnRevesed)
{
for (int i = carArray.Length; i != 0; i--)
{
yield return carArray[i-1];
}
}
else
{
// Return the items as placed in the array.
foreach (Car c in carArray)
{
yield return c;
}
}
}
Notice that our new method allows the caller to obtain the subitems in a sequential order, as
well as in reverse order, if the incoming parameter has the value true. We could now interact with
our new method as follows:
static void Main(string[] args)
{
Console.WriteLine("***** Fun with the Yield Keyword *****\n");
Garage carLot = new Garage();
Do'stlaringiz bilan baham: |