C# list removeat then collapses.

I have a array of slots that I want to remove from a list. Using RemoveAt() that is…

When I remove an item the remaining list collapses, therefore the values of my array are off after one removal.

I can think of ways around this but I was wondering if anyone knows if there is a right way or best way?

Can I make some sort of Que, then just remove everything at once, then have it collapse afterward?

Create a copy of your array. Remove elements from it. Assign the result in place of initial array.

I’m guessing you’re going in ascending order through the elements removing values from the List.

When you do this, removing one means that every other after it is offset by one, then remove another and they’re off by another.

If you want to remove values from a List without worrying about that, go from the end to the beginning when removing them.

// C#
for(int i = myList.Count - 1; i >= 0; i--)
{
	myList.RemoveAt(i);
}