System.Collections.ArrayList Class

Implements the IList interface using an array whose size is dynamically increased as required.

See Also: ArrayList Members

Syntax

[System.Diagnostics.DebuggerDisplay("Count={Count}")]
[System.Diagnostics.DebuggerTypeProxy(typeof(System.Collections.CollectionDebuggerView))]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayList : IList, ICloneable

Remarks

For a strongly-typed alternative to ArrayList, consider using List`1. ArrayList may not always offer the best performance for a given task. See the "Performance Considerations" section in the List`1 reference topic for a discussion of the relative performance of these classes.

The ArrayList is not guaranteed to be sorted. You must sort the ArrayList prior to performing operations (such as erload:System.Collections.ArrayList.BinarySearch) that require the ArrayList to be sorted.

The capacity of an ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling ArrayList.TrimToSize or by setting the ArrayList.Capacity property explicitly.

For very large ArrayList objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.

Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

The ArrayList collection accepts null as a valid value, allows duplicate elements.

Using multidimensional arrays as elements in an ArrayList collection is not supported.

Thread Safety

This class is safe for multiple readers and no concurrent writers.

Example

The following example shows how to create, initialize, and display the values of a ArrayList.

C# Example

using System; 
using System.Collections; 

public class SamplesArrayList { 

  public static void Main() { 

  // Create and initialize a new ArrayList. 
  ArrayList myAL = new ArrayList(); 
  myAL.Add("Hello"); 
  myAL.Add("World"); 
  myAL.Add("!"); 

  // Display the properties and values of the ArrayList. 
  Console.WriteLine( "myAL" ); 
  Console.WriteLine( "Count: {0}", myAL.Count ); 
  Console.WriteLine( "Capacity: {0}", myAL.Capacity ); 
  Console.Write( "Values:" ); 
  PrintValues( myAL ); 
  } 

public static void PrintValues( IEnumerable myList ) { 

  IEnumerator myEnumerator = myList.GetEnumerator(); 
  while ( myEnumerator.MoveNext() ) 
    Console.Write( " {0}", myEnumerator.Current ); 
    Console.WriteLine(); 
  } 
}

The output is

myAL

Count: 3

Capacity: 16

Values: Hello World !

Requirements

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0