Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

ArrayList.Clear Method

Definition

Namespace:
System.Collections
Assemblies:
mscorlib.dll, System.Collections.NonGeneric.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Collections.NonGeneric.dll
Assembly:
System.Runtime.Extensions.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Removes all elements from the ArrayList.

public:
 virtual void Clear();
public virtual void Clear();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

Implements

Exceptions

The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Examples

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

using System;
using System.Collections;
public class SamplesArrayList {

 public static void Main() {

 // Creates and initializes a new ArrayList.
 ArrayList myAL = new ArrayList();
 myAL.Add( "The" );
 myAL.Add( "quick" );
 myAL.Add( "brown" );
 myAL.Add( "fox" );
 myAL.Add( "jumps" );

 // 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 );
 }

 public static void PrintValues( IEnumerable myList ) {
 foreach ( Object obj in myList )
 Console.Write( " {0}", obj );
 Console.WriteLine();
 }
}
/*
This code produces the following output.

Initially,
 Count : 5
 Capacity : 16
 Values: The quick brown fox jumps
After TrimToSize,
 Count : 5
 Capacity : 5
 Values: The quick brown fox jumps
After Clear,
 Count : 0
 Capacity : 5
 Values:
After the second TrimToSize,
 Count : 0
 Capacity : 16
 Values:
*/
Imports System.Collections

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("jumps")
 
 ' 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

End Class

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

Remarks

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.

Applies to

See also


Feedback

Was this page helpful?