In C#, Queue.Clear() removes every item from a Queue<T> in one call. Afterward Count is 0, and the queue is ready to use again.
Imagine the ticket counter closes for the day and everyone in line is sent home at once. That is faster than calling Dequeue() in a loop, and you do not need to read the removed items. The queue object itself stays alive, so you can Enqueue() new items right away.
This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.Clear(...). A Queue<T> is a class, so two variables can share one queue. It is first in, first out (FIFO): items leave in the order they arrived. It is not thread-safe; for several threads use ConcurrentQueue<T>.
Syntax
Call Clear with no arguments. It returns nothing.
C#
Queue<int> numbers = new Queue<int>();
numbers.Enqueue(1);
numbers.Enqueue(2);
numbers.Clear();
int count = numbers.Count; // 0
Call
What it does
numbers.Clear()
Removes all items. Count becomes 0.
numbers.Clear() (already empty)
Safe. Nothing happens and no exception is thrown.
numbers = new Queue<int>();
Another way to start fresh, but it makes a new queue. Other variables that point to the old queue still see the old items.
numbers.TrimExcess()
After Clear, release the unused storage too. See TrimExcess().
Clear is O(n) for types that hold references, because it must reset the slots so the garbage collector can free those objects. It keeps the queue's capacity (the internal array), so refilling it later is cheap. Calling Clear inside a foreach over the same queue makes the next loop step throw InvalidOperationException, since the queue was changed.
Library declarations
C#
public void Clear();
Parameters
Parameter
Description
None
Clear() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. After the call, Count is 0 and Peek() or Dequeue() would throw until you add items again.
C# Examples
Example 1 — Empty a Queue and Use It Again
Fill a queue, clear it, and check Count. Then add a new item to prove the queue still works.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
line.Enqueue("Ben");
line.Enqueue("Chitra");
line.Clear();
Console.WriteLine(line.Count);
Console.WriteLine(line.Contains("Ben"));
line.Enqueue("Dev");
Console.WriteLine(line.Count);
Console.WriteLine(line.Peek());
}
}
Output
0
False
1
Dev
How It Works
Three names in. The queue starts with Asha, Ben, and Chitra waiting.
One call, all gone. After Clear(), Count is 0 and Contains("Ben") is False. Nobody is left.
Still the same object. The variable line still points to a working queue. Nothing needs to be re-created.
Fresh start. Enqueueing "Dev" gives a Count of 1, and Dev is the front because the old items are no longer there.
Example 2 — Cancel Everything That Is Still Waiting
Serve the first two jobs, then something goes wrong and the rest should be dropped. Clear the leftovers and count what was cancelled.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> jobs = new Queue<int>(new int[] { 1, 2, 3, 4, 5 });
for (int i = 0; i < 2; i++) {
Console.WriteLine("Done job " + jobs.Dequeue());
}
int cancelled = jobs.Count;
jobs.Clear();
Console.WriteLine("Cancelled " + cancelled);
Console.WriteLine(jobs.Count);
}
}
Output
Done job 1
Done job 2
Cancelled 3
0
How It Works
Five jobs queued. The constructor fills the queue from an array, with job 1 at the front.
Two are served. The for loop runs twice, and each pass removes and prints the current front job.
Save the number first.Clear() does not return anything, so the code stores Count (which is 3) in cancelledbefore clearing.
Drop the rest. One Clear() call removes jobs 3, 4, and 5 together. The queue is empty, so the last line prints 0.
Example 3 — Two Variables, One Queue
A queue is a class, so two variables can point to the same object. See how Clear and assigning a new queue behave differently.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Queue<int> original = new Queue<int>();
original.Enqueue(7);
original.Enqueue(8);
Queue<int> alias = original;
alias.Clear();
Console.WriteLine(original.Count);
original.Enqueue(9);
Queue<int> other = original;
original = new Queue<int>();
Console.WriteLine(original.Count);
Console.WriteLine(other.Count);
}
}
Output
0
0
1
How It Works
One queue, two names.alias = original copies the reference, not the items. Both variables control the same queue.
Clear is visible everywhere. Clearing through alias empties the shared queue, so original.Count prints 0.
A new queue is different. After other keeps the old queue (holding 9), original = new Queue<int>() points original at a brand-new empty queue.
Result.original.Count is 0 (the new queue) and other.Count is 1 (the old one still has the 9). Clear changes the object; assignment only changes one variable.
Frequently Asked Questions
It removes all items from the queue. After the call Count is 0, and the queue object can be used again.
Not the internal array. Clear keeps the capacity so refilling is cheap. Call TrimExcess if you also want to release the unused storage.
Yes, in most cases, and it is much shorter to write. Use Dequeue in a loop only when you need to process each item as it leaves.
Clear empties the existing queue object, so every variable that points to it sees the change. new Queue<T>() creates another queue, and the old one stays untouched if something else references it.
Yes. It does nothing and throws no exception, so you do not need a Count check first.