Export (0) Print
Expand All

Stack.Clear Method

Removes all objects from the Stack.

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

'Declaration
Public Overridable Sub Clear
'Usage
Dim instance As Stack

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

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

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

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 obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class


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


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

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

    public static void PrintValues(IEnumerable myCollection)
    {
        IEnumerator objEnum = myCollection.GetEnumerator();
        while (objEnum.MoveNext()) {
            Console.Write("    {0}", objEnum.get_Current());
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesStack

/* 
 This code produces the following output.
 
 Initially,
    Count    : 5
    Values:    jumped    fox    brown    quick    The
 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

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft