I have defined the following extension method:
public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action)
{
foreach (T obj in sequence)
{
action(obj);
}
}
I can then use it as:
new [] {1, 2, 3} // an IEnumerable<T>
.ForEach(n =>
{
// do something
});
I want to be able to take advantage of continue
and break
inside my extension method so that I can do:
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
});
Can these keywords only be used within a for
, foreach
, while
or do-while
loops?