Clear Method
Collapse the table of content
Expand the table of content

Stack.Clear Method ()

 

Removes all objects from the Stack.

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

Public Overridable Sub Clear

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:

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft