Queue.Clear Method in C#

The Queue.Clear() method in C# is used to remove all elements from a Queue<T> collection. This method provides an efficient way to empty the entire queue in a single operation, setting the count to zero.

Syntax

Following is the syntax for the Clear() method −

public void Clear();

Parameters

The Clear() method does not take any parameters.

Return Value

The method does not return any value. It is a void method that modifies the queue in place.

Queue.Clear() Operation Before Clear() 100 200 300 400 Count = 4 Clear() After Clear() Empty Queue Count = 0 All elements removed in one operation Memory is freed and Count becomes 0

Using Queue.Clear() to Empty a Queue

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<int> queue = new Queue<int>();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      queue.Enqueue(400);
      queue.Enqueue(500);
      
      Console.WriteLine("Queue before Clear():");
      foreach(int i in queue) {
         Console.WriteLine(i);
      }
      Console.WriteLine("Count of elements = " + queue.Count);
      
      queue.Clear();
      
      Console.WriteLine("\nQueue after Clear():");
      Console.WriteLine("Count of elements = " + queue.Count);
      Console.WriteLine("Is queue empty? " + (queue.Count == 0));
   }
}

The output of the above code is −

Queue before Clear():
100
200
300
400
500
Count of elements = 5

Queue after Clear():
Count of elements = 0
Is queue empty? True

Using Clear() with String Queue

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Queue<string> queue = new Queue<string>();
      queue.Enqueue("Apple");
      queue.Enqueue("Banana");
      queue.Enqueue("Cherry");
      
      Console.WriteLine("Original queue:");
      foreach(string fruit in queue) {
         Console.WriteLine(fruit);
      }
      Console.WriteLine("Count: " + queue.Count);
      
      // Add more elements
      queue.Enqueue("Date");
      queue.Enqueue("Elderberry");
      Console.WriteLine("\nAfter adding more elements:");
      Console.WriteLine("Count: " + queue.Count);
      
      // Clear all elements
      queue.Clear();
      Console.WriteLine("\nAfter Clear():");
      Console.WriteLine("Count: " + queue.Count);
      
      // Add elements after clearing
      queue.Enqueue("Fig");
      Console.WriteLine("\nAfter adding new element:");
      Console.WriteLine("Count: " + queue.Count);
   }
}

The output of the above code is −

Original queue:
Apple
Banana
Cherry
Count: 3

After adding more elements:
Count: 5

After Clear():
Count: 0

After adding new element:
Count: 1

Common Use Cases

  • Resetting a queue: When you need to start fresh with an empty queue without creating a new instance.

  • Memory management: Clearing references to objects to help with garbage collection.

  • Batch processing: Clearing a queue after processing all items in a batch operation.

Conclusion

The Queue.Clear() method provides an efficient way to remove all elements from a queue collection in C#. It sets the count to zero and helps free memory by removing references to all contained objects, making it ideal for resetting queues or batch processing scenarios.

Updated on: 2026-03-17T07:04:36+05:30

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements