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 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

I seem to run into this very often. I need to build a Hash
I don't edit CSS very often, and almost every time I need to go
We get this error in Visual Studio 2005 and TFS very often. Can anyone
Very often when working on an ASP.NET web site, the options View Code and
CSS and Javascript files don't change very often, so I want them to be
Courses for people who are being introduced to programming very often include a code
after a bit speedtracing I found a piece of code (called very very often)
Very simply put: I have a class that consists mostly of static public members,
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
Very simple question, is there any cloud server enviroments avaliable these days for us

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.