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

前端 未结 12 921
自闭症患者
自闭症患者 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条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 15:17

    Do you have LINQ available to you? Your logic seems similar to Any:

    bool any = blackList.Any(s=>inputString.Contains(s));
    

    which is the same as:

    bool any = blackList.Any(inputString.Contains);
    

    If you don't have LINQ, then this is still the same as:

    bool any = blackList.Find(inputString.Contains) != null;
    

    If you want to run additional logic, there are things you can do (with LINQ) with TakeWhile etc

提交回复
热议问题