Queue<T> · C#
Peek / TryPeek
Returns the front element without removing it.
public T Peek(); public bool TryPeek(out T result)
Complexity
| Best case | O(1) |
|---|---|
| Worst case | O(1) |
| Extra space | O(1) |
One array read at the head index.
Tested example
var queue = new Queue<int>([7, 8]);
Assert.Equal(7, queue.Peek()); // reads the front, removes nothing
Assert.Equal(2, queue.Count);
Behaviour
- Returns
- The front element.
- Errors and preconditions
- Peek: InvalidOperationException if empty.
- Changes
- Nothing.
- Memory and invalidation
- No allocation.