Export (0) Print
Expand All

Queue.Clear Method

Removes all objects from the Queue.

[Visual Basic]
Public Overridable Sub Clear()
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

Remarks

Count is set to zero. To reset the capacity of the Queue, call TrimToSize. Trimming an empty Queue sets the capacity of the Queue to the default capacity, not zero.

Example

[Visual Basic, C#, C++] The following example shows how to clear the values of the Queue.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesQueue    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        myQ.Enqueue("jumped")
        
        ' Displays the count and values of the Queue.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myQ.Count)
        Console.Write("   Values:")
        PrintValues(myQ)
        
        ' Clears the Queue.
        myQ.Clear()
        
        ' Displays the count and values of the Queue.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myQ.Count)
        Console.Write("   Values:")
        PrintValues(myQ)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Values:    The    quick    brown    fox    jumped
' After Clear,
'    Count    : 0
'    Values: 

[C#] 
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );
      myQ.Enqueue( "jumped" );

      // Displays the count and values of the Queue.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", myQ.Count );
      Console.Write( "   Values:" );
      PrintValues( myQ );

      // Clears the Queue.
      myQ.Clear();

      // Displays the count and values of the Queue.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", myQ.Count );
      Console.Write( "   Values:" );
      PrintValues( myQ );
   }


   public static void PrintValues( IEnumerable myCollection )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
   Count    : 5
   Values:    The    quick    brown    fox    jumped
After Clear,
   Count    : 0
   Values:
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myCollection )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( S"\t{0}", myEnumerator->Current );
   Console::WriteLine();
}

int main()  {

   // Creates and initializes a new Queue.
   Queue* myQ = new Queue();
   myQ->Enqueue( S"The" );
   myQ->Enqueue( S"quick" );
   myQ->Enqueue( S"brown" );
   myQ->Enqueue( S"fox" );
   myQ->Enqueue( S"jumped" );

   // Displays the count and values of the Queue.
   Console::WriteLine( S"Initially," );
   Console::WriteLine( S"   Count    : {0}", __box(myQ->Count) );
   Console::Write( S"   Values:" );
   PrintValues( myQ );

   // Clears the Queue.
   myQ->Clear();

   // Displays the count and values of the Queue.
   Console::WriteLine( S"After Clear," );
   Console::WriteLine( S"   Count    : {0}", __box(myQ->Count) );
   Console::Write( S"   Values:" );
   PrintValues( myQ );
}

 /*
 This code produces the following output.

 Initially,
    Count    : 5
    Values:    The    quick    brown    fox    jumped
 After Clear,
    Count    : 0
    Values:
 */

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Queue Class | Queue Members | System.Collections Namespace | TrimToSize

Show:
© 2014 Microsoft