c# exit generic ForEach that use lambda
问题 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. 回答1: 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