Export (0) Print
Expand All

Queue.Clear Method

Removes all objects from the Queue.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual void Clear ()
public void Clear ()
public function Clear ()
Not applicable.

Count is set to zero, and references to other objects from elements of the collection are also released.

The capacity remains unchanged. To reset the capacity of the Queue, call TrimToSize. Trimming an empty Queue sets the capacity of the Queue to the default capacity.

This method is an O(n) operation, where n is Count.

The following example shows how to clear the values of the Queue.

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( Queue myQ )  {
      foreach ( Object myObj in myQ )  {
         Console.Write( "    {0}", myObj );
      }
      Console.WriteLine();
   }

}
/* 
This code produces the following output.

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

*/ 

import System.*;
import System.Collections.*;

public class SamplesQueue
{
    public static void main(String[] args)
    {
        // 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}",
            System.Convert.ToString( myQ.get_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}",System.Convert.ToString(myQ.get_Count()));
        Console.Write("   Values:");
        PrintValues(myQ);
    } //main

    public static void PrintValues(Queue myQ) 
    {
        IEnumerator e = myQ.GetEnumerator();
        while (e.MoveNext()) {
            Object myObj = e.get_Current();
            Console.Write("    {0}", myObj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesQueue 

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

 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft