Queue<T>
Struct · namespace System · Since 0.09
struct Queue<T>A queue of values: first in, first out.
var jobs = Queue<string>.Create();jobs.Enqueue("load");jobs.Enqueue("draw");string next = jobs.Dequeue(); // "load"if (jobs.TryDequeue() is string s) // no panic when the queue is empty ...Like List<T>, a Queue is a small handle to shared storage: copies see the same elements. foreach and Get(i) go from the front (the next Dequeue) to the back. The elements are kept in a ring buffer, so Enqueue and Dequeue do not move the other elements.
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Create |
A new, empty queue. |
Create
Section titled “Create”static Queue<T> Create()A new, empty queue.
static Queue<T> Create(int capacity)A new, empty queue with room for capacity elements before it has to grow.
Methods
Section titled “Methods”| Name | Description |
|---|---|
IsCreated |
Whether the queue has storage: made by Create(), not the zero value (which cannot change). |
Count |
The number of elements. |
Enqueue |
Adds value at the back of the queue. |
Dequeue |
Removes and returns the front element. |
TryDequeue |
Removes and returns the front element, if there is one. |
Peek |
The front element, without removing it. |
TryPeek |
The front element without removing it, if there is one. |
Get |
The element index places behind the front (0 is the front). |
Contains |
Whether an element equals value (IEquatable). |
Clear |
Removes all elements. |
ToArray |
The elements from the front to the back (the order Dequeue would return them). |
IsCreated
Section titled “IsCreated”bool IsCreated()Whether the queue has storage: made by Create(), not the zero value (which cannot change).
Since 0.26
int Count()The number of elements.
Enqueue
Section titled “Enqueue”void Enqueue(T value)Adds value at the back of the queue.
Panics when the queue was not created (Queue<T>.IsCreated).
Dequeue
Section titled “Dequeue”T Dequeue()Removes and returns the front element.
Panics when the queue is empty (Queue<T>.TryDequeue does not).
TryDequeue
Section titled “TryDequeue”Optional<T> TryDequeue()Removes and returns the front element, if there is one.
Returns nothing (null) when the queue is empty.
T Peek()The front element, without removing it.
Panics when the queue is empty (Queue<T>.TryPeek does not).
TryPeek
Section titled “TryPeek”Optional<T> TryPeek()The front element without removing it, if there is one.
Returns nothing (null) when the queue is empty.
T Get(int index)The element index places behind the front (0 is the front).
Panics when index is not in 0 to Count() - 1.
Contains
Section titled “Contains”bool Contains(T value) where T : IEquatable<T>Whether an element equals value (IEquatable).
void Clear()Removes all elements.
ToArray
Section titled “ToArray”T[] ToArray()The elements from the front to the back (the order Dequeue would return them).