How to remove an item from the list correctly

Possible duplicates:
Exception during iteration when collecting and removing items from this collection
How to remove items from the general list during iteration around it?
Best way to remove matching items from a list

// tmpClientList is List<Client> type if (txtboxClientName.Text != "") foreach (Client cli in tmpClientList) if (cli.Name != txtboxClientName.Text) tmpClientList.Remove(cli); 

Error: "The collection has been modified; the enumeration operation may not be performed."

How can I remove elements from a list in a simple way without storing the indices of these elements in another list or array and deleting them elsewhere in the code. I also tried RemoveAt (index), but this is exactly the same situation, changing when the loop starts.

+6
c #
source share
7 answers

The problem is that you are trying to change the list in the foreach iteration. Replace it with a and you should be fine.

Also, since you seem to be using user input for the name, consider clearing the input a bit, at least with Trim () to remove the extra spaces. If you do not, John and John will be two different things. The same goes for the initial! = "" Test.

+1
source share

Moving backward through the list .. thus deleting an item does not affect the next item.

 for(var i=tmpClientList.Count-1;i>=0;i--) { if (tmpClientList[i].Name != txtboxClientName.Text) tmpClientList.RemoveAt(i); } 
+11
source share

On the List<T> there is a RemoveAll that the delegate accepts to indicate whether to remove the item. You can use it as follows:

 tmpCLientList.RemoveAll(cli => cli.Name != txtboxClientName.Text); 
+11
source share

Use either a for / while loop or tmpClientList.RemoveAll(a => a.Name == txtboxClientName.Text) . Since you did not specify which version of C # you are using, ymmw.

+4
source share

Do not use foreach. Use for and drop the list (i.e., start from the end) using RemoveAt.

So,

 // tmpClientList is List<Client> type if (txtboxClientName.Text != "") foreach (int pos = tmpClientList.Length - 1; pos >= 0; pos--) { Client cli = tmpClientList[pos]; if (cli.Name != txtboxClientName.Text) tmpClientList.RemoveAt(pos); } 
+2
source share

You can create another list with the items you want to remove and iterate over the new list to remove items from the "txtboxClientName" list.

+1
source share

In fact, foreach uses Enumerators to iterate through Item-Collections data. Next, System.Collections.Generic.List<T> implements IEnumarable -Interface to provide a class that knows how to iterate over list items, i.e. Enumerator . Now, if you iterate over this list using foreach, Enumerator keeps track of the current position, how to get to the next position and some other things. Internal logic can be something like storing the number of elements in a variable n, and then accessing all objects from 0 to n-1. As you can see, if any object is deleted between iteration steps, we will end up with a NullReferenceException when the Enumerator tries to deliver the last object of the list. Therefore, to prevent any iterative crashes, the list itself cannot be changed during enumeration.

I hope I was able to formulate this, at least a little comprehensively. :-)

+1
source share

All Articles