Does anyone know if it is possible to exit a generic ForEach that uses lambda? e.g.
someList.ForEach(sl =>
{
if (sl.ToString() == "foo")
break;
// continue processing sl here
// some processing code
}
);
This code itself won't compile. I know I could use a regular foreach but for consistency I want to use lambda.
Many thanks.
Sure. But first, note that I recommend against this; I say that a sequence operator should not have a side effect, and a statement should have a side effect. If you're doing something in that ForEach lambda, then make it a statement in the body of a foreach loop rather than making it look like a sequence operator.
That said, here's what you do. First, you write yourself a ForEach that works on arbitrary sequences, not just lists:
And now you write your break like this:
Warning: the code below is not to be taken seriously and is provided for entertainment purposes only!
You can 'simulate' a continue with an early return like this:
That said, I think that side effects within lambda's are a sign that you're doing it wrong. Use a proper foreach instead. Or something like TakeWhile, as Eric kindly demonstrated already.
How about this?
From MSDN
Don't know if that helps given the code you posted. The relevant quote is from the end of the MSDN article.