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:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Queue q = new Queue();
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(2);
q.Enqueue(4);
Console.Write( "Total number of elements " +
"in the Queue are : " );
Console.WriteLine(q.Count);
q.Clear();
Console.Write( "Total number of elements" +
" in the Queue are : " );
Console.WriteLine(q.Count);
}
}
|
Output:
Total number of elements in the Queue are : 4
Total number of elements in the Queue are : 0
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Queue q = new Queue();
q.Enqueue( "C" );
q.Enqueue( "C++" );
q.Enqueue( "Java" );
q.Enqueue( "PHP" );
q.Enqueue( "HTML" );
q.Enqueue( "Python" );
Console.Write( "Total number of elements " +
"in the Queue are : " );
Console.WriteLine(q.Count);
q.Clear();
Console.Write( "Total number of elements " +
"in the Queue are : " );
Console.WriteLine(q.Count);
}
}
|
Output:
Total number of elements in the Queue are : 6
Total number of elements in the Queue are : 0
Reference:
Last Updated :
04 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...