Queue Class in C#
In C#, the Queue<T> class is the part of the System.Collections.Generic namespace and represent a first-in-first-out (FIFO) collection of objects.
When we add an item to the list, it is called enqueue and when we remove an item, it is called dequeue.
Syntax
Queue<string> queue = new Queue<string>();
// Enqueue elements (FIFO order)
queue.Enqueue("First");
queue.Enqueue("Second");
- Enqueue adds an element to the end of the Queue.
- Dequeue removes the oldest element from the start of the Queue.
- Peek returns the oldest element that is at the start of the Queue but does not remove it from the Queue.
- The capacity of a Queue is the number of elements the Queue can hold.
- As elements are added to a Queue, the capacity is automatically increased as required by reallocating the internal array.
- Queue accepts null as a valid value for reference types and allows duplicate elements.
Declaration of Queue Class
1. Generic Queue: Holds elements of a specific type.
Queue<int> q = new Queue<int>(); // Queue of integers
2. Non-Generic Queue: Can hold elements of any type but requires casting when retrieving elements.
Queue q = new Queue(); // Non-generic queue
Example: Program to show generic and non-generic Queue usage
using System;
using System.Collections;
using System.Collections.Generic;
public class Geeks {
static public void Main()
{
// Non-generic Queue (can hold any type)
Queue q = new Queue();
q.Enqueue("GFG");
q.Enqueue(1);
q.Enqueue(10);
q.Enqueue(null);
q.Enqueue(2.4);
q.Enqueue("Geeks123");
Console.WriteLine("Non-Generic Queue:");
foreach(var item in q) {
Console.WriteLine(item);
}
Console.WriteLine();
// Generic Queue (holds only strings)
Queue<string> genericQueue = new Queue<string>();
genericQueue.Enqueue("GFG");
genericQueue.Enqueue("GeeksForGeeks");
genericQueue.Enqueue("Queue Example");
Console.WriteLine("Generic Queue:");
foreach(var item in genericQueue) {
Console.WriteLine(item);
}
}
}
Output
GFG 1 10 2.4 Geeks123
Constructors
The Queue<T> class provides four constructor which are listed below in the table:
Constructor | Description |
---|---|
Initializes an empty queue with the default capacity and growth factor. | |
Queue(ICollection) | Initializes a queue that contains elements copied from the specified collection, with capacity equal to the number of elements copied. |
Queue(Int32) | Initializes an empty queue with the specified initial capacity and the default growth factor. |
Queue(Int32,Single) | Initializes an empty queue with the specified initial capacity and growth factor. |
Example: This example demonstrates the total number of elements present in the queue.
using System;
using System.Collections;
class Geeks {
public static void Main(){
// Creating a Queue
Queue q = new Queue();
// Inserting the elements into the Queue
q.Enqueue("one");
q.Enqueue("two");
q.Enqueue("three");
q.Enqueue("four");
q.Enqueue("five");
// Displaying the count of elements contained in the Queue
Console.Write("Total number of elements in the Queue are : ");
Console.WriteLine(q.Count);
}
}
Output
Total number of elements in the Queue are : 5
Properties
The Queue<T> class provides several properties to access its state.
Property | Description |
---|---|
Count | Gets the number of elements contained in the Queue. |
IsSynchronized | Gets a value indicating whether access to the Queue is synchronized (thread safe). |
SyncRoot | Gets an object that can be used to synchronize access to the Queue. |
Example: This example demonstrates how to use the SyncRoot property of a Queue to ensure thread safety.
using System;
using System.Threading;
using System.Collections;
namespace sync_root {
class Geeks {
static void Main(string[] args){
// Declaring an Queue
Queue q = new Queue();
// Adding elements to Queue
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
// Using the SyncRoot property
lock(q.SyncRoot){
// foreach loop to display the elements in q1
foreach(Object i in q) Console.WriteLine(i);
}
}
}
}
Output
1 2 3 4 5
Methods
Method | Description |
---|---|
Clear() | Removes all objects from the Queue. |
Clone() | Creates a shallow copy of the Queue. |
Contains(Object) | Determines whether an element is in the Queue. |
CopyTo(Array, Int32) | Copies the Queue elements to an existing one-dimensional Array, starting at the specified array index. |
Dequeue() | Removes and returns the object at the beginning of the Queue. |
Enqueue(Object) | Adds an object to the end of the Queue. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an enumerator that iterates through the Queue. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Peek() | Returns the object at the beginning of the Queue without removing it. |
Synchronized(Queue) | Returns a new Queue that wraps the original queue and is thread safe. |
ToArray() | Copies the Queue elements to a new array. |
ToString() | Returns a string that represents the current object. |
TrimToSize() | Sets the capacity to the actual number of elements in the Queue. |
Example: This example demonstrates that the specific element is present in the Queue using the Contains() method.
using System;
using System.Collections;
class Geeks {
public static void Main()
{
// Creating a Queue
Queue q = new Queue();
// Inserting the elements into the Queue
q.Enqueue(5);
q.Enqueue(10);
q.Enqueue(15);
q.Enqueue(20);
q.Enqueue(25);
// Checking whether the element is present in the Queue or not
Console.WriteLine("The queue contains 7 ? :" + q.Contains(7));
Console.WriteLine("The queue contains 10 ? :" + q.Contains(10));
}
}
Output
The queue contains 7 ? :False The queue contains 10 ? :True
Example: This example demonstrates how to convert a Queue to an object array using ToArray() method.
using System;
using System.Collections;
class Geeks {
public static void Main()
{
// Creating a Queue
Queue q = new Queue();
// Inserting the elements into the Queue
q.Enqueue("Geeks");
q.Enqueue("for");
q.Enqueue("Geeks");
// Converting the Queue into object array
Object[] arr = q.ToArray();
// Displaying the elements in array
foreach(Object i in arr){
Console.WriteLine(i);
}
}
}
Output
Geeks for Geeks