Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7248259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:06:51+00:00 2026-05-28T22:06:51+00:00

I have some list and I do list.ForEach( l => { … something …})

  • 0

I have some list and I do list.ForEach( l => { ... something ...}). Now, on certain condition I need to stop iterating over the list, but break doesn’t work – I get “Control cannot leave the body of an anonymous method or lambda expression” compilation error.

Any idea how to overcome that restriction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:06:52+00:00Added an answer on May 28, 2026 at 10:06 pm

    Using break alone won’t work here because the lambda executes in a different method than the for loop. A break statement is only useful for breaking out of constructs local to the current function.

    In order to support a break style leave you’d need to add an overload of ForEach where the delegate can specify via a return value that loop execution should break. For example

    public static void ForEach<T>(this IEnumerable<T> enumerable, Func<T, bool> func) {
      foreach (var cur in enumerable) {
        if (!func(cur)) {
          break;
        }
      }
    }
    

    Now a consumer of this ForEach method can specify a break by returning false from the provided callback

    myCollection.ForEach(current => {
      if (someCondition) {
        // Need to break
        return false;
      }
      // Keep going
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code, int count = 0; list.ForEach(i => i.SomeFunction(count++)); This seems to
I have some std::list<char> list_type Now I have to supply contents of the list
I have some text i need to filter out a list of bad words
Suppose I have some code that looks like this: foreach(type x in list y)
let's say I have a foreach loop to perform some processing on List MyTypeInstanveVariable
If I have some R list mylist , you can append an item obj
I have some LINQ code that generates a list of strings, like this: var
I have some code that inserts a list item into a list... I then
We have some code which sorts a list of addresses based on the distance
I have some C structures related to a 'list' data structure. They look like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.