我定义了以下扩展方法:
public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action)
{
foreach (T obj in sequence)
{
action(obj);
}
}
然后我可以将其用作:
new [] {1, 2, 3} // an IEnumerable<T>
.ForEach(n =>
{
// do something
});
我希望能够利用continue
并break
在我的扩展方法内部,这样我就可以做到:
new [] {1, 2, 3}
.ForEach(n =>
{
// this is an overly simplified example
// the n==1 can be any conditional statement
// I know in this case I could have just used .Where
if(n == 1) { continue; }
if(n == -1) { break; }
// do something
});
这些关键字只能在for
、或循环foreach
中使用吗?while
do-while