So, I need an array of items. And I was wondering which one would be the fastest/best to use (in c#), I’ll be doing to following things:
- Adding elements at the end
- Removing elements at the start
- Looking at the first and last element (every frame)
- Clearing it occasionally
- Converting it to a normal array (not a list. I’m using iTween and it asks a normal array.) I’ll do this almost every frame.
So, what would be the best to use considering these things? Especially the last one, since I’m doing that every frame. Should I just use an array, or is there something else that converts very fast to array and also has easy adding/removing of elements at the start & end?
Requirements 1) and 2) point to a
Queue<T>, it is the only standard collection optimized for these 2 operations.3) You’ll need a little trickery for getting at the Last element, First is
Peek().4) is simple (
.Clear())5) The standard
.ToArray()method will do this.You will not escape copying all elements (
O(n)) for item 5)