c# remove item from list while iterating code example
Example 1: Remove elements from a list while iterating over it in C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main()
{
List<int> list = new List<int>(Enumerable.Range(1, 10));
foreach (int item in list.Reverse<int>())
{
list.Remove(item);
}
}
}
Example 2: c# best way to loop and remove
for (int i = safePendingList.Count - 1; i >= 0; i--)
{
// some code
// safePendingList.RemoveAt(i);
}