Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify or delete items from an enumerable collection while iterating through it in C#

I have to delete some rows from a data table. I've heard that it is not ok to change a collection while iterating through it. So instead of a for loop in which I check if a row meets the demands for deletion and then mark it as deleted, I should first iterate through the data table and add all of the rows in a list, then iterate through the list and mark the rows for deletions. What are the reasons for this, and what alternatives do I have (instead of using the rows list I mean)?.

like image 255
kjv Avatar asked Nov 21 '08 11:11

kjv


People also ask

Can we modify collection while iterating?

It is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances.

Can you modify a collection element value while iterating using for each loop?

Use Iterator#remove . This is the only safe way to modify a collection during iteration. For more information, see The Collection Interface tutorial. If you also need the ability to add elements while iterating, use a ListIterator .

Can we modify collection while iterating C#?

Collections support foreach statement using Enumarator. Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.


2 Answers

Iterating Backwards through the List sounds like a better approach, because if you remove an element and other elements "fall into the gap", that does not matter because you have already looked at those. Also, you do not have to worry about your counter variable becoming larger than the .Count.

        List<int> test = new List<int>();         test.Add(1);         test.Add(2);         test.Add(3);         test.Add(4);         test.Add(5);         test.Add(6);         test.Add(7);         test.Add(8);         for (int i = test.Count-1; i > -1; i--)         {             if(someCondition){                 test.RemoveAt(i);             }         } 
like image 88
Michael Stum Avatar answered Oct 09 '22 02:10

Michael Stum


Taking @bruno code, I'd do it backwards.

Because when you move backwards, the missing array indices do not interfere with the order of your loop.

var l = new List<int>(new int[] { 0, 1, 2, 3, 4, 5, 6 });  for (int i = l.Count - 1; i >= 0; i--)     if (l[i] % 2 == 0)         l.RemoveAt(i);  foreach (var i in l) {     Console.WriteLine(i); } 

But seriuosly, these days, I'd use LINQ:

var l = new List<int>(new int[] { 0, 1, 2, 3, 4, 5, 6 });  l.RemoveAll(n => n % 2 == 0); 
like image 33
chakrit Avatar answered Oct 09 '22 04:10

chakrit