Queue.Clear Method in C#

This method is used to remove the objects from the Queue. This method is an O(n) operation, where n is the total count of elements. And this method comes under System.Collections namespace.

Syntax:

public void Clear ();

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to illustrate the
// Queue.Clear Method
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue q = new Queue();
  
        // Inserting the elements into the Queue
        q.Enqueue(1);
        q.Enqueue(2);
        q.Enqueue(2);
        q.Enqueue(4);
  
        // Displaying the count of elements
        // contained in the Queue before
        // removing all the elements
        Console.Write("Total number of elements "+
                           "in the Queue are : ");
  
        Console.WriteLine(q.Count);
  
        // Removing all elements from Queue
        q.Clear();
  
        // Displaying the count of elements
        // contained in the Queue after
        // removing all the elements
        Console.Write("Total number of elements"+
                         " in the Queue are : ");
  
        Console.WriteLine(q.Count);
    }
}

chevron_right


Output:



Total number of elements in the Queue are : 4
Total number of elements in the Queue are : 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to illustrate the
// Queue.Clear Method
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Queue
        Queue q = new Queue();
   
        // Inserting the elements into the Queue
        q.Enqueue("C");
        q.Enqueue("C++");
        q.Enqueue("Java");
        q.Enqueue("PHP");
        q.Enqueue("HTML");
        q.Enqueue("Python");
  
        // Displaying the count of elements
        // contained in the Queue before
        // removing all the elements
        Console.Write("Total number of elements "+
                           "in the Queue are : ");
  
        Console.WriteLine(q.Count);
  
        // Removing all elements from Queue
        q.Clear();
  
        // Displaying the count of elements
        // contained in the Queue after
        // removing all the elements
        Console.Write("Total number of elements "+
                           "in the Queue are : ");
  
        Console.WriteLine(q.Count);
    }
}

chevron_right


Output:

Total number of elements in the Queue are : 6
Total number of elements in the Queue are : 0

Reference:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.