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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:01:10+00:00 2026-05-16T09:01:10+00:00

foreach(BruteforceEntry be in Entries.Values) { if (be.AddedTimeRemove <= now) Entries.Remove(be.IPAddress); else if (be.Unbantime <=

  • 0
foreach(BruteforceEntry be in Entries.Values)
{
    if (be.AddedTimeRemove <= now)
        Entries.Remove(be.IPAddress);
    else if (be.Unbantime <= now && be.Unbantime.Day == DateTime.Now.Day)
        Entries.Remove(be.IPAddress);
}

An exception was thrown:

Collection was modified; enumeration operation may not execute.

For some reason, it is not any more.

I know you cannot remove something, while iterating through it this way. My question is: How do I solve it?

  • 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-16T09:01:10+00:00Added an answer on May 16, 2026 at 9:01 am

    You can’t modify a collection you’re iterating over. In this case, a much better solution would be to create a list of entries to remove by iterating over the dictionary, and then iterate over that list, removing the entries from the dictionary:

    List<string> removals = new List<string>();                    
    DateTime now = DateTime.Now;
    foreach(BruteforceEntry be in Entries.Values)
    {
        if (be.AddedTimeRemove <= now ||
            (be.Unbantime <= now && be.Unbantime.Day == DateTime.Now.Day))
        {
            removals.Add(be.IPAddress);
        }
    }
    foreach (string address in removals)
    {
        Entries.Remove(address);
    }
    

    Note that if you’re using .NET 3.5, you could use a LINQ query to express the first part:

    List<string> removals = (from be in Entries.Values
                             where be.AddedTimeRemove <= now ||
                                   (be.Unbantime <= now && 
                                    be.Unbantime.Day == DateTime.Now.Day)
                             select be.IPAddress).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

foreach((array)$arr['subarr'] as &$foo) .... ...doesn't not work. It throws a parse error. Why?
I'm using foreach ($objs as $obj) { $data[] = $obj->getValue; } to collect values
I have a foreach loop (see below) that encodes the values to htmlentities() and
foreach (UIElement el in GridBoard.Children.ToList()) { if (el is Ellipse) { GridBoard.Children.Remove(el); } }
foreach ($topicarray as $key=>$value){ $files = mysql_query(mysqlquery); while($file = mysql_fetch_array($files)){ extract($file); $topicarray[$value] = array(
foreach (Control c in panPrev.Controls) { if (c.Tag == move) works well, but produce
foreach (GridViewRow row in GridView1.Rows) { var chk = row.FindControl(myCheckBox) as CheckBox; if (chk.Checked)
foreach (var person in peopleList.Where(person => person.FirstName == Messi)) { selectPeople.Add(person); } I am
foreach ($arr as $k => $v) { $r = my_func($v); $new[$k] = $r; }
foreach (string sn in MACOrSerial.Split(',')) { MACOrSerial = sn.Trim(); } MACORSerial contains a string

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.