tags:

views:

110

answers:

5

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

+5  A: 

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>.

Brian R. Bondy
Be aware that the Queue<T> class does this, but in reverse of the OP's specified order. This is only a partially true answer.
Reed Copsey
+2  A: 

How about Queue<T>?

spender
+1  A: 

you could use the Queue class. It should provide all the methods you need.

luke
+1  A: 

Yes Queue<T>

Cory Charlton
Heh:answered 43 secs ago Brian R. Bondyanswered 16 secs ago spenderanswered 4 secs ago lukeanswered 0 secs ago Cory CharltonBrian wins ;-)
C. Ross
+1  A: 

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.

Reed Copsey
I think he was describing a queue just with a bit strange wording.
Brian R. Bondy
@Brian: Not sure. He definitely specified adding at the front, and removing from the "back", which is the opposite of how Queue<T> is implemented. It may or may not matter (I tried to clarify this in my answer), but if it does, Queue<T> will be wrong.
Reed Copsey
@Brian: I question it mainly because he did use the word queue, so I'm assuming the OP knows about queues. :)
Reed Copsey