Class Queue
Represents a first-in, first-out (FIFO) collection of objects.
Inheritance
Object
CollectionBase
Queue
Implements
ICollection, IEnumerable, ICloneable
Members
Constructors
| Name | Description |
|---|---|
Queue() |
Initializes a new instance of the Queue class that is empty and has the default initial capacity. |
Queue(Int32 capacity) |
Initializes a new instance of the Queue class that is empty and has the specified initial capacity or the default initial capacity if the specified capacity is less than one. |
Queue(Int32 capacity, Single growFactor) |
Initializes a new instance of the Queue class with the specified initial capacity and growth factor. |
Queue(ICollection c) |
Initializes a new instance of the Queue class that contains elements copied from the specified collection and has enough capacity to accommodate the number of elements copied. |
Methods
| Name | Description |
|---|---|
Clear() |
Removes all objects from the Queue. |
Clone() |
Creates a shallow copy of the Queue. |
Contains(Object value) |
Determines whether an element is in the Queue. |
CopyTo(Array array, Int32 index) |
Copies the Queue elements to a one-dimensional Array, starting at the specified index. |
Dequeue() |
Removes and returns the object at the beginning of the Queue. |
Enqueue(Object value) |
Adds an object to the end of the Queue. |
GetEnumerator() |
Returns an enumerator that iterates through the Queue. |
Peek() |
Returns the object at the beginning of the Queue without removing it. |
ToArray() |
Copies the elements of the Queue to a new array. |
TrimToSize() |
Sets the capacity of the Queue to its actual size. |
Properties
| Name | Description |
|---|---|
Capacity |
Gets or sets the total number of elements the internal data structure can hold without resizing. |
Count |
Gets the number of elements contained in the Queue. |
Examples
The following example demonstrates how to use the Queue class to manage a collection of items in a FIFO order.
using System;
using System.Collections;
public class Example
{
public static void Main()
{
// Create a new Queue.
Queue myQueue = new Queue();
// Enqueue some items.
myQueue.Enqueue("Hello");
myQueue.Enqueue(123);
myQueue.Enqueue(true);
myQueue.Enqueue("World");
// Get the number of items in the queue.
Console.WriteLine("Number of items: {0}", myQueue.Count); // Output: 4
// Dequeue items.
while (myQueue.Count > 0)
{
object obj = myQueue.Dequeue();
Console.WriteLine(obj);
}
// Output:
// Hello
// 123
// True
// World
// Clear the queue.
myQueue.Clear();
Console.WriteLine("Number of items after Clear: {0}", myQueue.Count); // Output: 0
}
}
Remarks
A Queue is a collection that represents a first-in, first-out (FIFO) queue. Objects are enqueued to the end and dequeued from the front.
The Queue class is a non-generic collection that can only store objects of type Object.
If you need a generic collection that can store elements of any type, use the System.Collections.Generic.Queue<T> class.
The Queue is not synchronized, meaning that multiple threads can access it concurrently. This can lead to unexpected results if not handled properly. For thread-safe collections, consider using the System.Collections.Concurrent namespace.
The Capacity property represents the number of elements that the internal array of the Queue can hold.
The Count property represents the actual number of elements currently in the Queue.
Inheritance
Object
CollectionBase
Queue
Implements
ICollection, IEnumerable, ICloneable