Export (0) Print
Expand All

ArrayList.Clear Method

Removes all elements from the ArrayList.

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

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

instance.Clear()

Implements

IList.Clear

ExceptionCondition
NotSupportedException

The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

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

Capacity remains unchanged. To reset the capacity of the ArrayList, call TrimToSize or set the Capacity property directly. Trimming an empty ArrayList sets the capacity of the ArrayList to the default capacity.

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

The following code example shows how to trim the unused portions of the ArrayList and how to clear the values of the ArrayList.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    

    Public Shared Sub Main()

        ' Creates and initializes a new ArrayList. 
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("quick")
        myAL.Add("brown")
        myAL.Add("fox")
        myAL.Add("jumped")

        ' Displays the count, capacity and values of the ArrayList.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myAL.Count)
        Console.WriteLine("   Capacity : {0}", myAL.Capacity)
        Console.Write("   Values:")
        PrintValues(myAL)

        ' Trim the ArrayList.
        myAL.TrimToSize()

        ' Displays the count, capacity and values of the ArrayList.
        Console.WriteLine("After TrimToSize,")
        Console.WriteLine("   Count    : {0}", myAL.Count)
        Console.WriteLine("   Capacity : {0}", myAL.Capacity)
        Console.Write("   Values:")
        PrintValues(myAL)

        ' Clear the ArrayList.
        myAL.Clear()

        ' Displays the count, capacity and values of the ArrayList.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myAL.Count)
        Console.WriteLine("   Capacity : {0}", myAL.Capacity)
        Console.Write("   Values:")
        PrintValues(myAL)

        ' Trim the ArrayList again.
        myAL.TrimToSize()

        ' Displays the count, capacity and values of the ArrayList.
        Console.WriteLine("After the second TrimToSize,")
        Console.WriteLine("   Count    : {0}", myAL.Count)
        Console.WriteLine("   Capacity : {0}", myAL.Capacity)
        Console.Write("   Values:")
        PrintValues(myAL)
    End Sub 

    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class 

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft