Hello, I have a list with 5 elements...What I want to move foward all elements, removing the last one and add a new value to the first one. Is there any pre-made list methods that do that or help me so? Like a Queue
You can use Queue<T>
Edit: Your wording can be interpreted a couple different ways but if you want a first in first out behavior then use a Queue<T>
. If you want a first in last out behavior then use a Stack<T>
.
You could use a Queue<T>
, as suggested numerous times. Be aware, however, that Queue will add items to the end, and remove from the front, which is the opposite behavior that you specified. If you're using this collection as an IEnumerable<T>
and enumerating it, the Queue<T>
will be backwards from your specifications.
There are two ways to work around this easily. You could iterate the queue in reverse if you treat this as an enumerable by using Enumerable.Reverse().
Alternatively, you could use a LinkedList<T>
. This allows you to insert and remove from both the beginning and the end of the collection. This would let you preserve the ordering you specified.
If the order doesn't matter, and you just need a general purpose queue, then Queue<T>
is definitely the best choice.