Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, April 30, 2010 2:49 PM | 1 vote
When using a for loop it's easy to figure out what index to shove into the RemoveAt method on a generic list. But I'm not sure how to do this in a foreach so easily:
foreach (Item item in Items)
{
if(item == null)
// remove it from the Items generic list
....
}
C# Web Developer
All replies (8)
Friday, April 30, 2010 2:53 PM âś…Answered | 4 votes
It's not possible. The underlying collection cannot be modified while it's being enumerated.
The standard approach is to keep track of the items to remove (e.g., in another list), and then after Items has been enumerated, enumerate the removelist, removing each item from Items.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
and How to Implement IDisposable and Finalizers: 3 Easy Rules
Microsoft Certified Professional Developer
How to get to Heaven according to the Bible
Friday, April 30, 2010 2:56 PM | 1 vote
For Each loops are read only. You cannot alter the content of the target list in a For Each loop. Use a standard For loop.
Friday, April 30, 2010 3:11 PM | 14 votes
There are actually several ways to do this:
1) You can use a for/next loop instead a for/each.
2) You can iterate through a separate list:
List<Customer> custList = Customer.Populate();
foreach (var cust in custList.ToList())
{
custList.Remove(cust);
}
Notice the ToList on the list variable. So this iterates through the list created by the ToList but removes the items from the original list.
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
Tuesday, January 21, 2014 10:48 AM | 4 votes
Can't you just use linq to do this for you, ie:
Items.RemoveAll(item => item == null);
Then your above example would be:
var Items = new List<string>{"A","b",null,"c"};
foreach (string item in Items)
{
if(item == null)
Debug.WriteLine("delete me");
}
Items.RemoveAll(item => item == null);
foreach (string item in Items)
{
Debug.WriteLine(item);
}
Sunday, February 23, 2014 12:21 AM | 1 vote
Hello
A reverse 'forr' can do the trick.
Wednesday, March 19, 2014 7:14 PM
Brilliant!
Wednesday, March 19, 2014 8:51 PM | 2 votes
Typically this is done with a For loop and you will usually do the for loop in starting with the last element working your way to the 0 element and removing items along the way.
While there are other examples shown here some of them are inefficient.
static void Main(string[] args)
{
var s = new List<string>() { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
for (int i = s.Count -1; i >= 0; i--)
{
if ((int) Convert.ToChar(s[i])%2 == 1)
{
s.RemoveAt(i);
}
}
Console.ReadKey();
}
Wednesday, March 19, 2014 10:04 PM
items = items.Where(item => item!=null);
Paul Linton