How can I use continue statement in .ForEach() method
Asked Answered
G

6

45

Is there an equivalent to the continue statement in ForEach method?

List<string> lst = GetIdList();
lst.ForEach(id =>
{
     try
     {
       var article = GetArticle(id);
       if (article.author.contains("Twain"))
       {
         //want to jump out of the foreach now
         //continue; **************this is what i want to do*******

       }

       //other code follows
   }

EDIT: Thanks for all the great answers. And thank you for the clarification that .foreach is not an extension method. I use this structure to keep the coding style consistent (a different programmer worked on another similar method in the same class)...and thanks for the links to why to avoid using .foreach.

Gouty answered 30/11, 2011 at 21:49 Comment(3)
if you want to use foraech so bad, use regular foreach insteadMartini
At the risk of overemphasizing, please please please please please use a standard foreach loop.Lager
Recheck the term "continue". Inside a loop, 'continue' means "stop processing this item and go to the next". The keyword to exit processing completely is "break"Flint
F
28

Personally, I would just use a standard foreach loop instead of List<T>.ForEach.

In this case, you can invert the condition (to avoid the code in that case) or call return, since your goal is to use a continue statement. However, if you wanted to break, this would not work. That being said, there are quite a few other reasons to avoid List<T>.ForEach, so I would consider switching this to a normal foreach statement.

Forest answered 30/11, 2011 at 21:54 Comment(0)
P
85

A) ForEach is not LINQ, it is a method on List<T>.
B) Just use foreach.
C) return will do it.

Edit

Just to clarify, what you are doing is providing a method that will be called for each entry in the list. return will just apply to the method for that member. The rest of the members in the list will still call the method.

Ploch answered 30/11, 2011 at 21:52 Comment(0)
F
28

Personally, I would just use a standard foreach loop instead of List<T>.ForEach.

In this case, you can invert the condition (to avoid the code in that case) or call return, since your goal is to use a continue statement. However, if you wanted to break, this would not work. That being said, there are quite a few other reasons to avoid List<T>.ForEach, so I would consider switching this to a normal foreach statement.

Forest answered 30/11, 2011 at 21:54 Comment(0)
H
14

Just invert your if condition:

lst.ForEach(id => {
                      var article = GetArticle(id);
                      if (!article.author.contains("Twain"))
                      {
                          // other code here
                      }
                  });
Horodko answered 30/11, 2011 at 21:51 Comment(0)
R
4

The method you're using is List<T>.ForEach, a method defined on the class and not a LINQ extension method. This question actually has nothing to do with LINQ.

return; in the delegate passed to List<T>.ForEach should approximate using continue; in an actual foreach construct.

Rapids answered 30/11, 2011 at 21:57 Comment(0)
P
3

To answer your question:

List<string> lst = GetIdList();
lst.ForEach(id =>
{
    try
    {
        var article = GetArticle(id);
        if (article.author.contains("Twain"))
            goto _continue;
    }

    //other code follows

    _continue:;
}

The other answers are right, avoid .ForEach().
goto should be avoided as well, although it's sometimes useful for shortcutting code blocks.

Pondweed answered 10/5, 2017 at 12:20 Comment(0)
L
1

Continue in a foreach will loop back up to the top of the loop to the next item in the sequence. If that's what you want, return should do it. If you want to exit entirely, you may want to rewrite it using an actual LINQ method like Any().

Lockhart answered 30/11, 2011 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.