使用在迭代代码块中,提供枚举对象
...
{
public static IEnumerable Power(int number, int exponent)
...{
int counter = 0;
int result = 1;
while (counter++ < exponent)
...{
result = result * number;
yield return result;
}
}
public static int[] ints = ...{ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
public static IEnumerable xxx()
...{
for (int i=0; i<ints.Length; i++)
...{
yield return ints[i];
}
}
static void Main()
...{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
...{
Console.Write("{0} ", i);
}
foreach (int i in xxx())
...{
Console.Write("{0} ", i);
}
}
}
public static IEnumerable Power(int number, int exponent)
...{
int counter = 0;
int result = 1;
while (counter++ < exponent)
...{
result = result * number;
yield return result;
}
}
public static int[] ints = ...{ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
public static IEnumerable xxx()
...{
for (int i=0; i<ints.Length; i++)
...{
yield return ints[i];
}
}
static void Main()
...{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
...{
Console.Write("{0} ", i);
}
foreach (int i in xxx())
...{
Console.Write("{0} ", i);
}
}
}