Clear Method

Stack.Clear Method

Removes all objects from the Stack.

[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.

Example

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

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

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        myStack.Push("jumped")
        
        ' Displays the count and values of the Stack.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myStack.Count)
        Console.Write("   Values:")
        PrintValues(myStack)
        
        ' Clears the Stack.
        myStack.Clear()
        
        ' Displays the count and values of the Stack.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myStack.Count)
        Console.Write("   Values:")
        PrintValues(myStack)
    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:    jumped    fox    brown    quick    The
' After Clear,
'    Count    : 0
'    Values:
 

[C#] 
using System;
using System.Collections;

public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes a new Stack.
      Stack myStack = new Stack();
      myStack.Push( "The" );
      myStack.Push( "quick" );
      myStack.Push( "brown" );
      myStack.Push( "fox" );
      myStack.Push( "jumped" );

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

      // Clears the Stack.
      myStack.Clear();

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


   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:    jumped    fox    brown    quick    The
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 Stack.
   Stack* myStack = new Stack();
   myStack->Push( S"The" );
   myStack->Push( S"quick" );
   myStack->Push( S"brown" );
   myStack->Push( S"fox" );
   myStack->Push( S"jumped" );

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

   // Clears the Stack.
   myStack->Clear();

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

/*
This code produces the following output.

Initially,
   Count    : 5
   Values:      jumped  fox     brown   quick   The
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

See Also

Stack Class | Stack Members | System.Collections Namespace

Show:
© 2016 Microsoft