C# Queue (Queue)
In C#, Queue is a first-in, first-out (FIFO, First In First Out) data structure.
Queue belongs to the System.Collections or System.Collections.Generic namespace, providing non-generic and generic implementations respectively. Queue is suitable for scenarios where data needs to be processed in the order it was enqueued.
A queue (Queue) represents aFirst In First Outcollection of objects. Use a queue when you need first-in, first-out access to items. When you add an item to the list, it is calledenqueue, and when you remove an item from the list, it is calledDequeue。
Feature
- First In First Out: the element added first to the queue is removed first.
- Dynamic size: the capacity of the queue is dynamically adjusted as needed.
- Generic Support: Through
Queuecan store strongly typed elements. - Thread Safety:
Queueitself is not thread-safe, but you can useConcurrentQueueto achieve thread safety.
Queue Class Methods and Properties
The following table lists the Queue Some commonly used members of the class property:
| Attribute name | Type | Description |
|---|---|---|
Count |
int |
Gets the number of elements in the queue. |
SyncRoot |
object |
Gets an object used to synchronize access to the queue (non-generic). |
IsSynchronized |
bool |
Indicates whether access to the queue is synchronized (thread-safe, always false)。 |
The following table lists the Queue Some commonly used members of the class Method:
| Method Name | Return Type | Description |
|---|---|---|
| Element Operations | ||
Enqueue(object item) |
void |
Adds an element to the end of the queue. |
Dequeue() |
object |
Removes and returns the element at the beginning of the queue. |
Peek() |
object |
Returns the element at the beginning of the queue without removing it. |
Clear() |
void |
Removes all elements from the queue. |
| Checking and Copying | ||
Contains(object item) |
bool |
Determines whether an element is in the queue. |
ToArray() |
object[] |
Copies the elements of the queue to a new array. |
Clone() |
object |
Creates a shallow copy of the current queue. |
CopyTo(Array array, int index) |
void |
Copies the elements of the queue to an existing array, starting at a specified index. |
| Enumerator Support | ||
GetEnumerator() |
IEnumerator |
Returns an enumerator that iterates through the elements of the queue. |
| Thread Safety | ||
Synchronized(Queue queue) |
Queue |
Returns a thread-safe wrapper around the queue. |
Example
The following example demonstrates the use of a queue (Queue):
Example 1
using
System
;
using
System.Collections
;
class
Program
{
static
void
Main
(
)
{
Queue queue
=
new
Queue
(
)
;
// Add elements
queue
.
Enqueue
(
"First"
)
;
queue
.
Enqueue
(
"Second"
)
;
queue
.
Enqueue
(
"Third"
)
;
// View the front of the queue
Console
.
WriteLine
(
$
"Peek: {queue.Peek()}"
)
;
// Output: First
// Remove element
Console
.
WriteLine
(
$
"Dequeue: {queue.Dequeue()}"
)
;
// Output: First
// Remaining elements
foreach
(
var
item
in
queue
)
{
Console
.
WriteLine
(
item
)
;
// Output: Second, Third
}
// Check containment
Console
.
WriteLine
(
$
"Contains 'Second': {queue.Contains("
Second
")}"
)
;
// Output: True
// Convert to array
object
[
]
array
=
queue
.
ToArray
(
)
;
Console
.
WriteLine
(
$
"Array Length: {array.Length}"
)
;
// Output: 2
}
}
Example 2
using
System
;
using
System.Collections
;
namespace
CollectionsApplication
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
Queue q
=
new
Queue
(
)
;
q
.
Enqueue
(
'A'
)
;
q
.
Enqueue
(
'M'
)
;
q
.
Enqueue
(
'G'
)
;
q
.
Enqueue
(
'W'
)
;
Console
.
WriteLine
(
"Current queue: "
)
;
foreach
(
char
c
in
q
)
Console
.
Write
(
c
" "
)
;
Console
.
WriteLine
(
)
;
q
.
Enqueue
(
'V'
)
;
q
.
Enqueue
(
'H'
)
;
Console
.
WriteLine
(
"Current queue: "
)
;
foreach
(
char
c
in
q
)
Console
.
Write
(
c
" "
)
;
Console
.
WriteLine
(
)
;
Console
.
WriteLine
(
"Removing some values "
)
;
char
ch
=
(
char
)
q
.
Dequeue
(
)
;
Console
.
WriteLine
(
"The removed value: {0}"
, ch
)
;
ch
=
(
char
)
q
.
Dequeue
(
)
;
Console
.
WriteLine
(
"The removed value: {0}"
, ch
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Current queue:
A M G W
Current queue:
A M G W V H
Removing values
The removed value: A
The removed value: M
C# Collections