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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:21:11+00:00 2026-05-15T17:21:11+00:00

I want to do a foreach loop while taking out members of that foreach

  • 0

I want to do a foreach loop while taking out members of that foreach loop, but that’s throwing errors. My only idea is to create another list inside of this loop to find which Slices to remove, and loop through the new list to remove items from Pizza.

foreach(var Slice in Pizza)
{
    if(Slice.Flavor == "Sausage")
    {
        Me.Eat(Slice); //This removes an item from the list: "Pizza"
    }
}
  • 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-15T17:21:11+00:00Added an answer on May 15, 2026 at 5:21 pm

    You can do this, by far the simplest way I have found (like to think I invented it, sure that’s not true though ;))

    foreach (var Slice in Pizza.ToArray())
    {
        if (Slice.Flavor == "Sausage") // each to their own.. would have gone for BBQ
        {
            Me.Eat(Slice);
        }
    }
    

    ..because it’s iterating over a fixed copy of the loop. It will iterate all items, even if they are removed.

    Handy isn’t it!

    (By the way guys, this is a handy way of iterating through a copy of a collection, with thread safety, and removing the time an object is locked: Lock, get the ToArray() copy, release the lock, then iterate)

    Hope that helps!

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

Sidebar

Related Questions

I have a report in Access that we want split and exported to .rtf
I have code in php such as the following: while($r = mysql_fetch_array($q)) { //
How can I suspend a .NET DataGridView from displaying anything while I update its
My goal is pretty straightforward: I want to generate an HTML table with the
While similar to this question which gave me the LINQ for part of my
EDIT: Problem wasn't related to the question. It was indeed something wrong with my
I'm using a table to update a database, the table has 264 checkboxes which
I'd like something like this: each[i_, {1,2,3}, Print[i] ] Or, more generally, to destructure
I am currently working on a private messaging system. Right now I'm coding the
I'm attempting to write a function which starts a thread for each contact and

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.