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

  • Home
  • SEARCH
  • 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 3693884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:25:49+00:00 2026-05-19T04:25:49+00:00

Very often I need something like that: foreach (Line line in lines) { if

  • 0

Very often I need something like that:

 foreach (Line line in lines)
 {
    if (line.FullfilsCertainConditions())
    {
       lines.Remove(line)
    }
 }

This does not work, because I always get a InvalidOperationException because the Enumerator was changed during the loop.

So I changed all my loops of this kind to the following:

List<Line> remove = new List<Line>();
foreach (Line line in lines)
{
   if (line.FullfilsCertainConditions())
   {
      remove.Add(line)
   }
}

foreach (Line line in remove) {
{
   lines.Remove(line);
}

I’m not sure if this is really the best way since in the worst case I have to iterate 2 times over the original list and so it needs time 2n instead of n.

Is there a better way to do this?

EDIT:

I was able to do that using Mark’s answer!But what if my collection doesn’t implements RemoveAll()?

For example a

System.Windows.Controls.UIElementCollection

EDIT 2:

Again with the help of Mark I’m now able to make the following call to remove all ScatterViewItems:

CollectionUtils.RemoveAll(manager.getWindow().IconDisplay.Items, elem => elem.GetType() == typeof(ScatterViewItem));
  • 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-19T04:25:49+00:00Added an answer on May 19, 2026 at 4:25 am

    This is baked directly into List<T>:

    lines.RemoveAll(line => line.FullfilsCertainConditions());
    

    or in C# 2.0:

    lines.RemoveAll(delegate(Line line) {
        return line.FullfilsCertainConditions();
    });
    

    In the non-List<T> case (your edit to the question), you could wrap this something like below (untested):

    static class CollectionUtils
    {
        public static void RemoveAll<T>(IList<T> list, Predicate<T> predicate)
        {
            int count = list.Count;
            while (count-- > 0)
            {
                if (predicate(list[count])) list.RemoveAt(count);
            }
        }
        public static void RemoveAll(IList list, Predicate<object> predicate)
        {
            int count = list.Count;
            while (count-- > 0)
            {
                if (predicate(list[count])) list.RemoveAt(count);
            }
        }
    }
    

    Since UIElementCollection implements the (non-generic) IList this should work. And quite conveniently, with C# 3.0 you can add a this before IList / IList<T> and have it as an extension method. The only subtlety is that the parameter to the anon-method will be object, so you’ll need to cast it away.

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

Sidebar

Related Questions

This is a general kind of question. Very often, I need to write JavaScript
I work almost exclusively in the terminal, and very often I need to view
Often I need to minimise object allocations within code that runs very frequently. Of
Very often, it's natural to need to specify a CSS style for all elements
very often I like to have my movieclip code inside the movieclip (on a
I get this error in xml file very often. here is the code in
I have the following line of code called very often: var configValue = System.Configuration.ConfigurationManager.AppSettings[ConfigValueKey];
I'm finding this kind of overriding after a new in java code very often
I very often need to do some Emacs magic on some files and I
(NOT a possible duplicate of Swap text around equal sign :) Very often 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.