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

前端 未结 12 923
自闭症患者
自闭症患者 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:07

    I don't think there's an elegant way to do it when using the ForEach method. A hacky solution is to throw an exception.

    What's preventing you from doing an old fashioned foreach?

    foreach (string item in blackList)
    {
        if (!inputString.Contains(item)) continue;
    
        result = true;
        break;
    }
    

提交回复
热议问题