How do i exit a List.ForEach loop when using an anonymous delegate?

前端 未结 12 931
自闭症患者
自闭症患者 2020-12-14 14:24

In a normal loop you can break out of a loop using break. Can the same be done using an anonymous delegate?

Example inputString and result are both declared outside

12条回答
  •  猫巷女王i
    2020-12-14 14:59

    As others have posted, you can't exit the loop in ForEach.

    Are you able to use LINQ? If so, you could easily combine TakeWhile and a custom ForEach extension method (which just about every project seems to have these days).

    In your example, however, List.FindIndex would be the best alternative - but if you're not actually doing that, please post an example of what you really want to do.

提交回复
热议问题