Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove all objects from the Queue in C#
To remove all objects from a Queue in C#, use the Clear() method. This method removes all elements from the Queue and resets its count to zero. The Queue<T> class provides this built-in method for efficient bulk removal of all elements.
Syntax
Following is the syntax for the Clear() −
queue.Clear();
Where queue is the Queue object from which all elements will be removed.
Using Clear() Method
Example 1
The following example demonstrates clearing a Queue containing string elements −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("Gary");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
queue.Enqueue("Mark");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
Console.WriteLine("Count of elements = " + queue.Count);
queue.Clear();
Console.WriteLine("Count of elements (after Clear) = " + queue.Count);
}
}
The output of the above code is −
Count of elements = 8 Count of elements (after Clear) = 0
Example 2
Here's another example showing the Clear operation with a different set of elements −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("A");
queue.Enqueue("B");
queue.Enqueue("C");
queue.Enqueue("D");
queue.Enqueue("E");
Console.WriteLine("Count of elements = " + queue.Count);
queue.Clear();
Console.WriteLine("Count of elements (after Clear) = " + queue.Count);
}
}
The output of the above code is −
Count of elements = 5 Count of elements (after Clear) = 0
Working with Different Data Types
Example 3
The Clear() method works with Queues of any data type. Here's an example with integers −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<int> numberQueue = new Queue<int>();
numberQueue.Enqueue(10);
numberQueue.Enqueue(20);
numberQueue.Enqueue(30);
numberQueue.Enqueue(40);
Console.WriteLine("Initial count: " + numberQueue.Count);
Console.WriteLine("Queue contains elements: " + (numberQueue.Count > 0));
numberQueue.Clear();
Console.WriteLine("Count after Clear(): " + numberQueue.Count);
Console.WriteLine("Queue is empty: " + (numberQueue.Count == 0));
}
}
The output of the above code is −
Initial count: 4 Queue contains elements: True Count after Clear(): 0 Queue is empty: True
Key Points
-
The
Clear()method has O(n) time complexity where n is the number of elements. -
After calling
Clear(), the Queue'sCountproperty returns 0. -
The method does not return any value (void method).
-
It's safe to call
Clear()on an already empty Queue.
Conclusion
The Clear() method is the most efficient way to remove all elements from a Queue in C#. It immediately empties the Queue and resets its count to zero, making it ready for new elements to be enqueued.
