.NET Framework Class Library
ArrayList Class

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

To browse the .NET Framework source code for this type, see the Reference Source.

Namespace:   System.Collections
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_0_0_0%]> _
<[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_0_0_1%](True)> _
Public Class ArrayList _
	Implements [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_0_0_2%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_0_0_3%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_0_0_4%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_0_0_5%]
[[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_1_0_0%]]
[[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_1_0_1%](true)]
public class ArrayList : [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_1_0_2%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_1_0_3%], 
	[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_1_0_4%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_1_0_5%]
[[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_2_0_0%]]
[[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_2_0_1%](true)]
public ref class ArrayList : [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_2_0_2%], 
	[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_2_0_3%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_2_0_4%], [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_2_0_5%]
[<[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_3_0_0%]>]
[<[%$TOPIC/7x4b0a97_en-us_VS_110_4_0_3_0_1%](true)>]
type ArrayList =  
    class 
        interface [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_3_0_2%] 
        interface [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_3_0_3%] 
        interface [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_3_0_4%] 
        interface [%$TOPIC/7x4b0a97_en-us_VS_110_4_0_3_0_5%] 
    end

The ArrayList type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework ArrayListInitializes a new instance of the ArrayList class that is empty and has the default initial capacity.
Public method Supported by the XNA Framework ArrayList(ICollection)Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied.
Public method Supported by the XNA Framework ArrayList(Int32)Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework CapacityGets or sets the number of elements that the ArrayList can contain.
Public property Supported by the XNA Framework CountGets the number of elements actually contained in the ArrayList.
Public property Supported by the XNA Framework IsFixedSizeGets a value indicating whether the ArrayList has a fixed size.
Public property Supported by the XNA Framework IsReadOnlyGets a value indicating whether the ArrayList is read-only.
Public property Supported by the XNA Framework IsSynchronizedGets a value indicating whether access to the ArrayList is synchronized (thread safe).
Public property Supported by the XNA Framework ItemGets or sets the element at the specified index.
Public property Supported by the XNA Framework SyncRootGets an object that can be used to synchronize access to the ArrayList.
Top
Methods
  NameDescription
Public method Static member AdapterCreates an ArrayList wrapper for a specific IList.
Public method Supported by the XNA Framework AddAdds an object to the end of the ArrayList.
Public method Supported by the XNA Framework AddRangeAdds the elements of an ICollection to the end of the ArrayList.
Public method BinarySearch(Object)Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.
Public method BinarySearch(Object, IComparer)Searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
Public method Supported by the XNA Framework BinarySearch(Int32, Int32, Object, IComparer)Searches a range of elements in the sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
Public method Supported by the XNA Framework ClearRemoves all elements from the ArrayList.
Public method Supported by the XNA Framework CloneCreates a shallow copy of the ArrayList.
Public method Supported by the XNA Framework ContainsDetermines whether an element is in the ArrayList.
Public method Supported by the XNA Framework CopyTo(Array)Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
Public method Supported by the XNA Framework CopyTo(Array, Int32)Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Public method Supported by the XNA Framework CopyTo(Int32, Array, Int32, Int32)Copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Public method Supported by the XNA Framework Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Supported by the XNA Framework FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member FixedSize(ArrayList)Returns an ArrayList wrapper with a fixed size.
Public method Static member FixedSize(IList)Returns an IList wrapper with a fixed size.
Public method Supported by the XNA Framework GetEnumeratorReturns an enumerator for the entire ArrayList.
Public method GetEnumerator(Int32, Int32)Returns an enumerator for a range of elements in the ArrayList.
Public method Supported by the XNA Framework GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetRangeReturns an ArrayList which represents a subset of the elements in the source ArrayList.
Public method Supported by the XNA Framework GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework IndexOf(Object)Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
Public method IndexOf(Object, Int32)Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.
Public method Supported by the XNA Framework IndexOf(Object, Int32, Int32)Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
Public method Supported by the XNA Framework InsertInserts an element into the ArrayList at the specified index.
Public method Supported by the XNA Framework InsertRangeInserts the elements of a collection into the ArrayList at the specified index.
Public method LastIndexOf(Object)Searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.
Public method LastIndexOf(Object, Int32)Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index.
Public method LastIndexOf(Object, Int32, Int32)Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that contains the specified number of elements and ends at the specified index.
Protected method Supported by the XNA Framework MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member ReadOnly(ArrayList)Returns a read-only ArrayList wrapper.
Public method Static member ReadOnly(IList)Returns a read-only IList wrapper.
Public method Supported by the XNA Framework RemoveRemoves the first occurrence of a specific object from the ArrayList.
Public method Supported by the XNA Framework RemoveAtRemoves the element at the specified index of the ArrayList.
Public method Supported by the XNA Framework RemoveRangeRemoves a range of elements from the ArrayList.
Public method Static member RepeatReturns an ArrayList whose elements are copies of the specified value.
Public method Supported by the XNA Framework ReverseReverses the order of the elements in the entire ArrayList.
Public method Supported by the XNA Framework Reverse(Int32, Int32)Reverses the order of the elements in the specified range.
Public method SetRangeCopies the elements of a collection over a range of elements in the ArrayList.
Public method Supported by the XNA Framework SortSorts the elements in the entire ArrayList.
Public method Supported by the XNA Framework Sort(IComparer)Sorts the elements in the entire ArrayList using the specified comparer.
Public method Supported by the XNA Framework Sort(Int32, Int32, IComparer)Sorts the elements in a range of elements in ArrayList using the specified comparer.
Public method Static member Supported by the XNA Framework Synchronized(ArrayList)Returns an ArrayList wrapper that is synchronized (thread safe).
Public method Static member Synchronized(IList)Returns an IList wrapper that is synchronized (thread safe).
Public method Supported by the XNA Framework ToArrayCopies the elements of the ArrayList to a new Object array.
Public method Supported by the XNA Framework ToArray(Type)Copies the elements of the ArrayList to a new array of the specified element type.
Public method Supported by the XNA Framework ToStringReturns a string that represents the current object. (Inherited from Object.)
Public method Supported by the XNA Framework TrimToSizeSets the capacity to the actual number of elements in the ArrayList.
Top
Extension Methods
  NameDescription
Public Extension Method AsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Supported by the XNA Framework CastTResultCasts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method Supported by the XNA Framework OfTypeTResultFilters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Remarks
NoteNote

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

For a strongly-typed alternative to ArrayList, consider using ListT. ArrayList may not always offer the best performance for a given task. See the "Performance Considerations" section in the ListT 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 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 TrimToSize or by setting the 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 as a valid value, allows duplicate elements.

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

Examples

The following code example shows how to create and initialize an ArrayList and how to print out its values.

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("Hello")
        myAL.Add("World")
        myAL.Add("!")

        ' Displays 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)
    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 output similar to the following: 
'  
' myAL 
'     Count:    3 
'     Capacity: 4 
'     Values:   Hello   World   !
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

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

      // Displays 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 )  {
      foreach ( Object obj in myList )
         Console.Write( "   {0}", obj );
      Console.WriteLine();
   }

}


/* 
This code produces output similar to the following:

myAL
    Count:    3
    Capacity: 4
    Values:   Hello   World   !

*/
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{

   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "Hello" );
   myAL->Add( "World" );
   myAL->Add( "!" );

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

void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "   {0}", obj );
   }

   Console::WriteLine();
}

/* 
This code produces output similar to the following:

myAL
    Count:    3
    Capacity: 4
    Values:   Hello   World   !

*/
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Thread Safety

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

An ArrayList can support multiple readers concurrently, as long as the collection is not modified. To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.