Hi I am trying to remove all numbers that are divisible by two from the arrayList.The probleme is that after one element is removed I get an InvalidOperationException.
private ArrayList RemoveDivTwo(ArrayList list) {
int count = 0;
foreach(int i in list){
if ((i > 2) && (i % 2 == 0)) {
list.RemoveAt(count);
}
count++;
}
return list;
}
How can I solve this problem so I wont’t get an exception and be able to remove all elements divisible by two?
Try iterating over it this way.
You are no longer iterating over the list. So this should work.