[C #] collection modified; Enumeration operation may not be performed

Source: Internet
Author: User

Summary

I'm sure a lot of people are more familiar with this. This error occurs when you manipulate a collection that has already been modified.

Solutions

For example, with the following section of code, we create a collection and add 10 numbers to the collection, and then we iterate over the numbers.

 static  void  Main (string  [] args) {List  <int< /span>> lst = new  list<();  for  (int  i = 0 ; I < 10 ; I++ foreach  (var  item in   LST) {lst.            Remove (item);        } console.read (); }

Appeared .....

Is the method pits provided by the generic collection? I remember being in a hole a long time ago. Very confused, but also very simple, because if you remove an item, the number of elements of the collection is changed. At this point the element is re-arranged, the second element's index is changed from 1 to 0, and the latter moves forward in turn.

        Static voidMain (string[] args) {List<int> LST =Newlist<int>();  for(inti =0; I <Ten; i++) {lst.            ADD (i); }            varresult =lst; Console.WriteLine ("the Count of LST"+lst.            Count); Lst. Remove (0); Console.WriteLine ("the Count of LST"+lst.            Count);  for(inti =0; I < LST. Count; i++) {Console.WriteLine ("index: {0}, Value: {1}", I, lst[i]);        } console.read (); }

The above code adds 10 elements to the collection. It then outputs the count of the current collection and then removes the element with index 0. There should be no element 0 in the collection at this time. Then the number of elements in the output collection. Outputs the index and corresponding value in the collection at this time.

As you can see, 1, which is indexed to 1, moves forward, when his index becomes 0. Therefore, when using foreach removal, the collection is changed and is not allowed. Is there no way to operate it? Of course, it's not about removing a collection, just one less.

At this point, we can remove the tail element from the collection by the For loop, which removes the elements from the end of the queue, although the count of the collection has changed, but their index has not changed.

       Static voidMain (string[] args) {List<int> LST =Newlist<int>();  for(inti =0; I <Ten; i++) {lst.            ADD (i); }            varresult =lst; Console.WriteLine ("the Count of LST"+lst.            Count); Lst. Remove (0); Console.WriteLine ("the Count of LST"+lst.            Count);  for(inti = LST. Count-1; I >=0; i--) {Console.WriteLine ("index: {0}, Value: {1}", I, lst[i]); Console.WriteLine ("removed element: {0}", Lst[i]); Lst.            RemoveAt (i);        } console.read (); }

So are we just removing the conditions that can also be passed for the for loop? Of course you do, foreach. You're not the only one that can loop.

        Static voidMain (string[] args) {List<int> LST =Newlist<int>();  for(inti =0; I <Ten; i++) {lst.            ADD (i); }            varresult =lst; Console.WriteLine ("the Count of LST"+lst.            Count); Lst. Remove (0); Console.WriteLine ("the Count of LST"+lst.            Count);  for(inti =0; I < LST. Count; i++) {Console.WriteLine ("index: {0}, Value: {1}", I, lst[i]); if(Lst[i]%2==0) {Console.WriteLine ("removed element: {0}", Lst[i]); Lst.                RemoveAt (i);        }} console.read (); }

Summarize

Recently, someone just met, here is still a record.

[C #] collection modified; Enumeration operation may not be performed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.