Effective collection for inserts and absorption at the beginning

What collection would you recommend for code that often inserts and deletes objects only at the beginning of the collection.

Here are some examples to illustrate my requirements.

while (collection.Count != 0)
{
   object obj = collection[0];
   collection.RemoveAt(0);

   ...

   if (somethingWith(obj))
       collection.Insert(0, anotherObj);

   ...  
}

At positions other than 0, there are no inserts or abstractions. Collection is not sorted.

What would you recommend?

EDIT:

I really don't need to do anything with the collection. The collection is used to queue the objects that need to be processed (and the collection is populated during processing).

+3
source share
1 answer

It seems you only want to implement a LIFO container so that you can use Stack<T>:

while (stack.Count > 0) {
    object obj = stack.Pop();
    // ...
    if (SomethingWith(obj)) {
        stack.Push(anotherObj);
    }
}
+8
source

All Articles