break 结合使用,表示迭代结束。
例子:
yield return <expression>;
yield break;
Dispose 方法。
这类方法、运算符或访问器的体受以下约束的控制:
-
不允许不安全块。
-
out。
-
该语句可放在后跟 finally 块的 try 块中。
-
yield break 语句可放在 try 块或 catch 块中,但不能放在 finally 块中。
匿名方法(C# 编程指南)。
try 块中。
public class List
{
//using System.Collections;
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}
/*
Output:
2 4 8 16 32 64 128 256
*/