how properly remove item from list

Posted by dygi on Stack Overflow See other posts from Stack Overflow or by dygi
Published on 2010-06-09T12:49:43Z Indexed on 2010/06/09 12:52 UTC
Read the original article Hit count: 161

Filed under:
|
|
|
// tmpClientList is List<Client> type

if (txtboxClientName.Text != "")
    foreach (Client cli in tmpClientList)
        if (cli.Name != txtboxClientName.Text)
            tmpClientList.Remove(cli);

Error: "Collection was modified; enumeration operation may not execute."

How can i remove items from the list, in some simple way, without saving indexes of these items in another list or array, and removing them in another place in the code. Tried also RemoveAt(index) but it's exactly the same situation, modifying when loop runs.

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#3.0