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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:49:17+00:00 2026-05-17T14:49:17+00:00

This question is asked many a time in this forum. I know solution for

  • 0

This question is asked many a time in this forum. I know solution for the problem. But I am curious to know why “Enumeration operation can not execute when a collection is modified”

        List<string> list = new List<string>();

        list.Add("a");

        list.Add("b");

        int[] array = new int[6] { 1, 2, 3, 4, 5, 5 };

        HashSet<int> hashSet = new HashSet<int>();

        int i = 0;

        foreach (string s in list)
        {
            list[i] = "test";

            i++;
        }

But when I change list to list.toarray It works.

  • 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-17T14:49:18+00:00Added an answer on May 17, 2026 at 2:49 pm

    Microsoft specifies that any object which implements iEnumerable must void any existing enumerations when the object is modified. The general reason for this requirement is that, for many types of collections, it is difficult to ensure that an enumerator will behave sensibly when a collection is modified. For example, suppose a List holds five values (A,B,C,D,E) and an enumerator works by setting n to the number of elements, and then outputting element(0), element(1), etc. up to element(n-1). If, while the enumerator is enumerating element(2) [C], an element BB is inserted after element(1) [i.e. B], the enumerator might then proceed to output element (3) [which is C, again], then element 4 [D], and then decide it’s done since it output all five elements. That would be a bad situation (one element showed up twice, and one went missing).

    It is certainly reasonable that an enumerator should be invalidated if a collection is modified in a way that would prevent the enumerator from yielding sensible results. To my mind, however, enumerators that are capable of meeting the following contract should do so, even if a collection is modified, rather than throwing exceptions:

    1. Any item which exists throughout the duration of enumeration must be returned exactly once.
    2. Any item which exists for part of the duration of enumeration must be returned exactly once or not at all, but there is no requirement regarding which such items (if any) are returned.
    3. The sequence in which the items are returned must be a valid sequence for the enumerator (e.g. a SortedList must return items in sorted sequence; if an item gets added to the list which would precede an already-output item, that item must not be enumerated).
    4. For purposes of (1) and (2), an item which is removed shall be considered distinct from one which is added, even if the keys are identical; if an item’s key is changed, the item before the change is considered distinct from the item after.
    5. Repeated enumerations via Reset are not guaranteed to return the same items.

    The VB6-style Collection seems to comply with the above semantics, but none of the other standard collections do. Too bad–such requirements could be met reasonably well by some data structures, but would be sufficient to avoid the duplication of list data which is frequently necessary under the current rules.

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

Sidebar

Related Questions

Maybe this question has been asked many times before, but I never found a
I know this question has been asked before, but this was one and a
I have asked this question before - but I have spent some time thinking
You may think this question is like this question asked on StackOverflow earlier. But
This question is not for the faint of heart and will be time consuming..
This question was asked quite some time ago, and while it covers possible solutions
This question comes on the heels of the question asked here . The email
I saw this question asked about C# I would like an answer for PHP.
This question originally asked which is the best method for uploading files via SFTP
This question was asked by Ed Burns in his book 'Riding the Crest'. 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.