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 644507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:24:01+00:00 2026-05-13T21:24:01+00:00

This is more of a hypothetical question as I am using .NET 3.5 more

  • 0

This is more of a hypothetical question as I am using .NET 3.5 more and more along with lambda expressions and anonymous delegates. Take this simple example:

static void Main(string[] args)
{
   List<int> numList = new List<int>(int[] { 1, 3, 5, 7, 9, 11, 12, 13, 15 });

   numList.ForEach(i =>
   {
       if (i % 2 == 1)
           Console.Write(i);
       else
           return;
   });

   Console.ReadLine();
}

This will produce the output:

13579111315

Of course, what I’d really like it to do is to stop executing the ForEach function after 12, and not print 13 or 15. In a traditional foreach construct, you would have a break (instead of the return in my example), and the loop would end. However, a break is illegal in this case. You get the following message:

No enclosing loop out of which to break or continue

Is there a construct I could easily employ here to get the desired result, or is it just better to use a standard foreach loop in this case if you don’t intend to actually run the desired code on every single member of a list?

  • 1 1 Answer
  • 3 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-13T21:24:01+00:00Added an answer on May 13, 2026 at 9:24 pm

    Just use a standard foreach loop. This is almost always simpler and less confusing than using a lambda expression – you don’t get confusion over captured variables etc.

    You may wish to read Eric Lippert’s post on this as well.

    List<T>.ForEach is useful if you’ve been handed a delegate and you just want to execute it on each element in the list – but otherwise I’d stick with the normal language feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a hypothetical question. I am wondering if anyone knows if/how
This is probably going to be more of a discussion or hypothetical question, but
(This was meant to be a general hypothetical question, me whining that .NET was
this is more of a fundamental question at how apache/threading works. in this hypothetical
Ok, I'm going to try to make this more clear because my last question
I have a label on my asp.net page, it looks like this: more info
This is more of a design question. I have a template class, and I
This question is quite hypothetical but I'll ask anyway. Let's say you start a
This is a rather hypothetical question, but let's say I have 3 long parameters
This is more of a hypothetical whilst I'm debugging some code. Lets say I

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.