How do i exit a List.ForEach loop when usi

2019-02-05 12:15发布

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 the delegate.

blackList.ForEach(new Action<string>(
    delegate(string item)
    {
        if(inputString.Contains(item)==true)
        {
            result = true;
            // I want to break here
        }
    }
));

Edit: Thanks for the replies, I'm actually reading your book at the minute John :) Just for the record i hit this issue and switched back to a normal foreach loop but I posted this question to see if i missed something.

11条回答
贪生不怕死
2楼-- · 2019-02-05 12:58

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;
}
查看更多
趁早两清
3楼-- · 2019-02-05 12: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<T>.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.

查看更多
聊天终结者
4楼-- · 2019-02-05 13:00
    class Program
{
    static void Main(string[] args)
    {
        List<string> blackList = new List<string>(new[] { "jaime", "jhon", "febres", "velez" });
        string inputString = "febres";
        bool result = false;
        blackList.ForEach((item) =>
                              {
                                  Console.WriteLine("Executing");
                                  if (inputString.Contains(item))
                                  {
                                      result = true;
                                      Console.WriteLine("Founded!");
                                  }
                              },
                          () => result);
        Console.WriteLine(result);
        Console.ReadLine();
    }


}
public static class MyExtensions
{
    public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action, Func<bool> breakOn)
    {
        foreach (var item in enumerable)
        {
            action(item);
            if (breakOn())
            {
                break;
            }
        }
    }
}
查看更多
5楼-- · 2019-02-05 13:03
bool @break = false;

blackList.ForEach(item =>
 {  
    if(!@break && inputString.Contains(item))
     { @break = true;
       result = true;
     }

    if (@break) return;
    /* ... */
 });

Note that the above will still iterate through each item but return immediately. Of course, this way is probably not as good as a normal foreach.

查看更多
狗以群分
6楼-- · 2019-02-05 13:04

Would this work for you:

bool result = null != blackList.Find( item => inputString.Contains(item)) );
查看更多
登录 后发表回答