First of all: terminology. If you say "garbage list", people will think you are talking about the garbage collector. Let's call it the "dead list".
As you have probably discovered, hence your question, you cannot remove items from a collection while you are iterating through it. If your collection is a
List<> then the simplest way to remove items from it is:
for(int i = list.Count-1; i >= 0; --i)
Note that you iterate backwards. You can remove items while iterating forward, but it's messier and requires a
goto. You cannot do it with
You can either do the removal separately from your update loop. Or you can merge it into your update loop. Always do the
RemoveAt at the end of the loop - after that point in the loop,
list[i] cannot be considered valid (it becomes valid again on the next iteration).
Note, also, that each object has its own
IsDead flag (if you are using the disposal pattern, you could make it
IsDisposed) - you don't actually need to maintain a "dead list" at all.
Using a flag on each item is preferable for performance, as you avoid having to search through the list to do removal. And it is also preferable for design - it means that each object can easily check if it is dead - so you don't accidentally call any methods on it (if these objects are implementing the disposable pattern, they could throw
ObjectDisposedException in this case).
If you have a collection other than a
List<>, then removing dead items from that collection may still involve the creation of a dead list (as removal during iteration may not be possible). In my opinion, it is nicer, design-wise, to create the dead list right before it gets used, by iterating over the collection looking for
IsDead flags, rather than trying to maintain the list as objects are killed.
Once you are finished with a dead list, you should
Clear() it, and then keep it around to re-use it later on.