I just noticed something very awkward I tested both methods RemoveAt() and Remove(), but they don't work the same and I am wondering if someone can exmplain the reason why. Basically the only method that successfully removes a specific item from the list in this example is Remove().
List<int> testList = new List<int>{1,76,3,4,5,76,76,8};
public void RemoveElements()
{
for (int i = 0; i < testList.Count; i++)
{
//WORKS
testList.Remove(76); //A
// DOES NOT WORK
//if(testList[i] == 76) testList.Remove(testList[i]); //B
// DOES NOT WORK EITHER
// if(testList[i] == 76) testList.RemoveAt(i); //C
}
}
The output of A is:1,3,4,5,8 The output of B is:1,3,4,5,76,8 The output of C is:1,3,4,5,76,8
Thank you for your time!