System.Collections.ArrayList Class

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Implements the IList interface using an array whose size is dynamically increased as required.
C# Syntax:
[Serializable]
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
Thread Safety
Public static (non-instance) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

An ArrayList can safely 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 ArrayList.Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

Remarks
The capacity of an ArrayList is the number of elements the list 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.

Indexes in this collection are zero-based.

Example
The following example shows how to create and initialize an ArrayList and how to print out its values.
 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( "\tCount:    {0}", myAL.Count );
       Console.WriteLine( "\tCapacity: {0}", myAL.Capacity );
       Console.Write( "\tValues:" );
       PrintValues( myAL );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 myAL
     Count:    3
     Capacity: 16
     Values:    Hello    World    !
 */

    
See also:
System.Collections Namespace | IList

System.Collections.ArrayList Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.
ctor #2 Overloaded:
.ctor(ICollection c)

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.
ctor #3 Overloaded:
.ctor(int capacity)

Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.
Public Properties
Capacity Read-write

Gets or sets the number of elements that the ArrayList can contain.
Count Read-only

Gets the number of elements actually contained in the ArrayList.
IsFixedSize Read-only

Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly Read-only

Gets a value indicating whether the ArrayList is read-only.
IsSynchronized Read-only

Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
Item Read-write

Gets or sets the element at the specified index.
SyncRoot Read-only

Gets an object that can be used to synchronize access to the ArrayList.
Public Methods
Adapter Creates an ArrayList wrapper for a specific IList.
Add Adds an object to the end of the ArrayList.
AddRange Adds the elements of an ICollection to the end of the ArrayList.
BinarySearch Overloaded:
BinarySearch(object value)

Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.
BinarySearch Overloaded:
BinarySearch(object value, IComparer comparer)

Searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
BinarySearch Overloaded:
BinarySearch(int index, int count, object value, IComparer comparer)

Searches a section of the sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
Clear Removes all elements from the ArrayList.
Clone Creates a shallow copy of the ArrayList.
Contains Determines whether an element is in the ArrayList.
CopyTo Overloaded:
CopyTo(Array array)

Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
CopyTo Overloaded:
CopyTo(Array array, int arrayIndex)

Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
CopyTo Overloaded:
CopyTo(int index, Array array, int arrayIndex, int count)

Copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
FixedSize Overloaded:
FixedSize(ArrayList list)

Returns an ArrayList wrapper with a fixed size.
FixedSize Overloaded:
FixedSize(IList list)

Returns an IList wrapper with a fixed size.
GetEnumerator Overloaded:
GetEnumerator()

Returns an enumerator for the entire ArrayList.
GetEnumerator Overloaded:
GetEnumerator(int index, int count)

Returns an enumerator for a section of the ArrayList.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetRange Returns an ArrayList which represents a subset of the elements in the source ArrayList.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IndexOf Overloaded:
IndexOf(object value)

Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
IndexOf Overloaded:
IndexOf(object value, int startIndex)

Searches for the specified Object and returns the zero-based index of the first occurrence within the section of the ArrayList that extends from the specified index to the last element.
IndexOf Overloaded:
IndexOf(object value, int startIndex, int count)

Searches for the specified Object and returns the zero-based index of the first occurrence within the section of the ArrayList that starts at the specified index and contains the specified number of elements.
Insert Inserts an element into the ArrayList at the specified index.
InsertRange Inserts the elements of a collection into the ArrayList at the specified index.
LastIndexOf Overloaded:
LastIndexOf(object value)

Searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.
LastIndexOf Overloaded:
LastIndexOf(object value, int startIndex)

Searches for the specified Object and returns the zero-based index of the last occurrence within the section of the ArrayList that extends from the first element to the specified index.
LastIndexOf Overloaded:
LastIndexOf(object value, int startIndex, int count)

Searches for the specified Object and returns the zero-based index of the last occurrence within the section of the ArrayList that contains the specified number of elements and ends at the specified index.
ReadOnly Overloaded:
ReadOnly(ArrayList list)

Returns a read-only ArrayList wrapper.
ReadOnly Overloaded:
ReadOnly(IList list)

Returns a read-only IList wrapper.
Remove Removes the first occurrence of a specific object from the ArrayList.
RemoveAt Removes the element at the specified index of the ArrayList.
RemoveRange Removes a range of elements from the ArrayList.
Repeat Returns an ArrayList whose elements are copies of the specified value.
Reverse Overloaded:
Reverse()

Reverses the order of the elements in the entire ArrayList.
Reverse Overloaded:
Reverse(int index, int count)

Reverses the order of the elements in the specified range.
SetRange Copies the elements of a collection over a range of elements in the ArrayList.
Sort Overloaded:
Sort()

Sorts the elements in the entire ArrayList using the IComparable implementation of each element.
Sort Overloaded:
Sort(IComparer comparer)

Sorts the elements in the entire ArrayList using the specified comparer.
Sort Overloaded:
Sort(int index, int count, IComparer comparer)

Sorts the elements in a section of ArrayList using the specified comparer.
Synchronized Overloaded:
Synchronized(ArrayList list)

Returns an ArrayList wrapper that is synchronized (thread-safe).
Synchronized Overloaded:
Synchronized(IList list)

Returns an IList wrapper that is synchronized (thread-safe).
ToArray Overloaded:
ToArray()

Copies the elements of the ArrayList to a new Object array.
ToArray Overloaded:
ToArray(Type type)

Copies the elements of the ArrayList to a new array of the specified type.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
TrimToSize Sets the capacity to the actual number of elements in the ArrayList.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Collections.ArrayList Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public ArrayList();
Remarks
The initial capacity is the starting capacity of the new ArrayList. The default initial capacity for an ArrayList is 16.

If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled.

See also:
ArrayList.Capacity

Return to top


Overloaded ctor #2
Summary
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.
C# Syntax:
public ArrayList(
   ICollection c
);
Parameters:

c

The ICollection whose elements are copied to the new list.

Exceptions
Exception Type Condition
ArgumentNullException c is null.
Remarks
The initial capacity is the starting capacity of the new ArrayList. If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled.

The elements are copied onto the ArrayList in the same order they are read by the IEnumerator of the ICollection.

See also:
ICollection | ArrayList.Capacity

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.
C# Syntax:
public ArrayList(
   int capacity
);
Parameters:

capacity

The number of elements that the new list is initially capable of storing.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException capacity is less than zero.
Remarks
The initial capacity is the starting capacity of the new ArrayList. The default initial capacity for an ArrayList is 16. If the specified initial capacity is zero, the default initial capacity is used.

If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled. Therefore, if the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the ArrayList.

See also:
ArrayList.Capacity

Return to top


Property: Capacity (read-write)
Summary
Gets or sets the number of elements that the ArrayList can contain.
C# Syntax:
public virtual int Capacity {get; set;}
Exceptions
Exception Type Condition
ArgumentOutOfRangeException ArrayList.Capacity is set to a value that is less than ArrayList.Count.
Remarks
ArrayList.Capacity is the number of elements that the ArrayList is capable of storing. ArrayList.Count is the number of elements that are actually in the ArrayList.

ArrayList.Capacity is always greater than or equal to ArrayList.Count. If ArrayList.Count exceeds ArrayList.Capacity while adding elements, the capacity of the list is doubled by automatically reallocating the internal array.

When the value of ArrayList.Capacity is set explicitly, the internal array is also reallocated to accommodate the specified capacity. If ArrayList.Capacity is explicitly set to zero, the common language runtime sets it to the default capacity instead. The default capacity is 16.

See also:
ArrayList.Count

Return to top


Property: Count (read-only)
Summary
Gets the number of elements actually contained in the ArrayList.
C# Syntax:
public virtual int Count {get;}
Implements:
ICollection.Count
Remarks
ArrayList.Count is the number of elements that are actually in the ArrayList. ArrayList.Capacity is the number of elements that the ArrayList is capable of storing.

ArrayList.Count is always less than or equal to ArrayList.Capacity. If ArrayList.Count exceeds ArrayList.Capacity while adding elements, the capacity of the list is doubled by automatically reallocating the internal array before copying the old elements and adding the new elements.

See also:
ArrayList.Capacity

Return to top


Property: IsFixedSize (read-only)
Summary
Gets a value indicating whether the ArrayList has a fixed size.
C# Syntax:
public virtual bool IsFixedSize {get;}
Implements:
IList.IsFixedSize
Remarks
A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but it allows the modification of existing elements.
Example
The following example shows how to create a fixed-size wrapper around an 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Create a fixed-size wrapper around the ArrayList.
       ArrayList myFixedSizeAL = ArrayList.FixedSize( myAL );
 
       // Display whether the ArrayLists have a fixed size or not.
       Console.WriteLine( "myAL {0}.", myAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" );
       Console.WriteLine( "myFixedSizeAL {0}.", myFixedSizeAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" );
       Console.WriteLine();
 
       // Display both ArrayLists.
       Console.WriteLine( "Initially," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
 
       // Sort is allowed in the fixed-size ArrayList.
       myFixedSizeAL.Sort();
 
       // Display both ArrayLists.
       Console.WriteLine( "After Sort," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
 
       // Reverse is allowed in the fixed-size ArrayList.
       myFixedSizeAL.Reverse();
 
       // Display both ArrayLists.
       Console.WriteLine( "After Reverse," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
 
       // Add an element to the standard ArrayList.
       myAL.Add( "AddMe" );
 
       // Display both ArrayLists.
       Console.WriteLine( "After adding to the standard ArrayList," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
       Console.WriteLine();
 
       // Adding or inserting elements to the fixed-size ArrayList throws an exception.
       try  {
          myFixedSizeAL.Add( "AddMe2" );
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
       try  {
          myFixedSizeAL.Insert( 3, "InsertMe" );
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
    }
 
    public static void PrintValues( IEnumerable myList, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.
 
 myAL does not have a fixed size.
 myFixedSizeAL has a fixed size.
 
 Initially,
 Standard  : The quick brown fox jumped over the lazy dog
 Fixed size: The quick brown fox jumped over the lazy dog
 After Sort,
 Standard  : brown dog fox jumped lazy over quick the The
 Fixed size: brown dog fox jumped lazy over quick the The
 After Reverse,
 Standard  : The the quick over lazy jumped fox dog brown
 Fixed size: The the quick over lazy jumped fox dog brown
 After adding to the standard ArrayList,
 Standard  : The the quick over lazy jumped fox dog brown AddMe
 Fixed size: The the quick over lazy jumped fox dog brown AddMe
 
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Add(Object obj)
    at SamplesArrayList.Main()
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
    at SamplesArrayList.Main()
 */ 

    

Return to top


Property: IsReadOnly (read-only)
Summary
Gets a value indicating whether the ArrayList is read-only.
C# Syntax:
public virtual bool IsReadOnly {get;}
Implements:
IList.IsReadOnly
Remarks
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.
Example
The following example shows how to create a read-only wrapper around an ArrayList and how to determine if an ArrayList is read-only.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Inserts code to create and initialize a new ArrayList.
       ArrayList myAL = new ArrayList();
 
       // Creates a read-only copy of the ArrayList.
       ArrayList myReadOnlyAL = ArrayList.ReadOnly( myAL );
 
       // Displays whether the ArrayList is read-only or writable.
       Console.WriteLine( "myAL is {0}.", myAL.IsReadOnly ? "read only" : "writable" );
       Console.WriteLine( "myReadOnlyAL is {0}.", myReadOnlyAL.IsReadOnly ? "read only" : "writable" );

       // Adding to a read-only copy causes an exception. 
       try  {
          myReadOnlyAL.Add("Boo!");
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
    }
 }
 /* 
 This code produces the following output.
 
 myAL is writable.
 myReadOnlyAL is read only.
 Exception: System.NotSupportedException: Collection is read-only.
    at System.Collections.ReadOnlyArrayList.Add(Object obj)
    at SamplesArrayList.Main()
 */ 

    
See also:
ArrayList.ReadOnly

Return to top


Property: IsSynchronized (read-only)
Summary
Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).
C# Syntax:
public virtual bool IsSynchronized {get;}
Implements:
ICollection.IsSynchronized
Remarks
To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the ArrayList.Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

The following code example shows how to lock the collection using the ArrayList.SyncRoot during the entire enumeration:

              ArrayList myCollection = new ArrayList();
               lock( myCollection.SyncRoot ) {
               foreach ( Object item in myCollection ) {
               // Insert your code here.
               }
              }
            
Example
The following example shows how to synchronize an ArrayList, determine if an ArrayList is synchronized and use a synchronized 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" );
 
       // Creates a synchronized wrapper around the ArrayList.
       ArrayList mySyncdAL = ArrayList.Synchronized( myAL );
 
       // Displays the sychronization status of both ArrayLists.
       Console.WriteLine( "myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized" );
       Console.WriteLine( "mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized" );
    }
 }
 /* 
 This code produces the following output.
 
 myAL is not synchronized.
 mySyncdAL is synchronized.
 */ 

    
See also:
ArrayList.SyncRoot | ArrayList.Synchronized

Return to top


Property: Item (read-write)
Summary
Gets or sets the element at the specified index.
C# Syntax:
public virtual object this[int index] {get; set;}
Parameters:

index

The zero-based index of the element to get or set.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

index is equal to or greater than ArrayList.Count.

Implements:
IList.Item
Remarks
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index] .
See also:
ArrayList.Count

Return to top


Property: SyncRoot (read-only)
Summary
Gets an object that can be used to synchronize access to the ArrayList.
C# Syntax:
public virtual object SyncRoot {get;}
Implements:
ICollection.SyncRoot
Remarks
To create a synchronized version of the ArrayList, use the ArrayList.Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the ArrayList.SyncRoot property. The synchronizing code must perform operations on the ArrayList.SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

The following code example shows how to lock the collection using the ArrayList.SyncRoot during the entire enumeration:

              ArrayList myCollection = new ArrayList();
               lock( myCollection.SyncRoot ) {
               foreach ( Object item in myCollection ) {
               // Insert your code here.
               }
              }
            
See also:
ArrayList.IsSynchronized | ArrayList.Synchronized

Return to top


Method: Adapter(
   IList list
)
Summary
Creates an ArrayList wrapper for a specific IList.
C# Syntax:
public static ArrayList Adapter(
   IList list
);
Parameters:

list

The IList to wrap.

Return Value:
The ArrayList wrapper around the IList.
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
ArrayList.Adapter does not copy the contents of IList. Instead, it only creates an ArrayList wrapper around IList; therefore, changes to the IList also affect the ArrayList.

The ArrayList class provides generic ArrayList.Reverse, ArrayList.BinarySearch and ArrayList.Sort methods. This wrapper can be a means to use those methods on IList; however, performing these generic operations through the wrapper might be less efficient than operations applied directly on the IList.

See also:
IList | ArrayList.BinarySearch | ArrayList.Reverse | ArrayList.Sort

Return to top


Method: Add(
   object value
)
Summary
Adds an object to the end of the ArrayList.
C# Syntax:
public virtual int Add(
   object value
);
Parameters:

value

The Object to be added to the end of the ArrayList.

Return Value:
The ArrayList index at which the value has been added.
Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Implements:
IList.Add
Remarks
If ArrayList.Count already equals ArrayList.Capacity, the capacity of the list is doubled by automatically reallocating the internal array and copying the existing elements to the new array before the new element is added.

If ArrayList.Count is less than ArrayList.Capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is ArrayList.Count.

Example
The following example shows how to add elements to 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" );
 
       // Creates and initializes a new Queue.
       Queue myQueue = new Queue();
       myQueue.Enqueue( "jumped" );
       myQueue.Enqueue( "over" );
       myQueue.Enqueue( "the" );
       myQueue.Enqueue( "lazy" );
       myQueue.Enqueue( "dog" );
 
       // Displays the ArrayList and the Queue.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL, '\t' );
       Console.WriteLine( "The Queue initially contains the following:" );
       PrintValues( myQueue, '\t' );
 
       // Copies the Queue elements to the end of the ArrayList.
       myAL.AddRange( myQueue );
 
       // Displays the ArrayList.
       Console.WriteLine( "The ArrayList now contains the following:" );
       PrintValues( myAL, '\t' );
    }
 
    public static void PrintValues( IEnumerable myList, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox
 The Queue initially contains the following:
     jumped    over    the    lazy    dog
 The ArrayList now contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 */ 

    
See also:
ArrayList.AddRange | ArrayList.Insert | ArrayList.Remove | ArrayList.Count

Return to top


Method: AddRange(
   ICollection c
)
Summary
Adds the elements of an ICollection to the end of the ArrayList.
C# Syntax:
public virtual void AddRange(
   ICollection c
);
Parameters:

c

The ICollection whose elements should be added to the end of the ArrayList.

Exceptions
Exception Type Condition
ArgumentNullException c is null.
NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Remarks
If the new ArrayList.Count (the current ArrayList.Count plus the size of the collection) will be greater than ArrayList.Capacity, the capacity of the list is either doubled or increased to the new ArrayList.Count, whichever is greater. The internal array is automatically reallocated to accommodate the new elements and the existing elements are copied to the new array before the new elements are added.

If the ArrayList can accommodate the new elements without increasing the ArrayList.Capacity, this method is an O(n) operation, where n is the number of elements to be added. If the capacity needs to be increased to accommodate the new elements, this method becomes an O(n + m) operation, where n is the number of elements to be added and m is ArrayList.Count.

Example
The following example shows how to add elements to 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" );
 
       // Creates and initializes a new Queue.
       Queue myQueue = new Queue();
       myQueue.Enqueue( "jumped" );
       myQueue.Enqueue( "over" );
       myQueue.Enqueue( "the" );
       myQueue.Enqueue( "lazy" );
       myQueue.Enqueue( "dog" );
 
       // Displays the ArrayList and the Queue.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL, '\t' );
       Console.WriteLine( "The Queue initially contains the following:" );
       PrintValues( myQueue, '\t' );
 
       // Copies the Queue elements to the end of the ArrayList.
       myAL.AddRange( myQueue );
 
       // Displays the ArrayList.
       Console.WriteLine( "The ArrayList now contains the following:" );
       PrintValues( myAL, '\t' );
    }
 
    public static void PrintValues( IEnumerable myList, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox
 The Queue initially contains the following:
     jumped    over    the    lazy    dog
 The ArrayList now contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 */ 

    
See also:
ICollection | ArrayList.Capacity | ArrayList.Count | ArrayList.Add | ArrayList.InsertRange | ArrayList.SetRange | ArrayList.GetRange | ArrayList.RemoveRange

Return to top


Overloaded Method: BinarySearch(
   object value
)
Summary
Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.
C# Syntax:
public virtual int BinarySearch(
   object value
);
Parameters:

value

The Object to locate.

Return Value:
The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element.
Exceptions
Exception Type Condition
ArgumentException Neither value nor the elements of ArrayList implement the IComparable interface.

-or-

value is not of the same type as the elements of the ArrayList.

Remarks
The value parameter and each element of the ArrayList must implement the IComparable interface, which is used for comparisons. If the ArrayList is not already sorted according to the IComparable implementation, the result might be incorrect.

Comparing null with any type is allowed and does not generate an exception when using IComparable. When sorting, null is considered to be less than any other object.

If the ArrayList contains more than one element with the same value, the method returns only the index of the first occurrence.

If the ArrayList does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the ArrayList, this index should be used as the insertion point to maintain the sort order.

This method is an O(log n) operation, where n is the number of elements in the range.

Example
The following example shows how to use ArrayList.BinarySearch to locate a specific object in the ArrayList.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList.
       ArrayList myAL = new ArrayList();
       for ( int i = 0; i <= 4; i++ )
          myAL.Add( i*2 );
 
       // Displays the ArrayList.
       Console.WriteLine( "The Int32 ArrayList contains the following:" );
       PrintValues( myAL );
 
       // Locates a specific object that does not exist in the ArrayList.
       Object myObjectOdd = 3;
       FindMyObject( myAL, myObjectOdd );
 
       // Locates an object that exists in the ArrayList.
       Object myObjectEven = 6;
       FindMyObject( myAL, myObjectEven );
    }
 
    public static void FindMyObject( ArrayList myList, Object myObject )  {
       int myIndex=myList.BinarySearch( myObject );
       if ( myIndex < 0 )
          Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex );
       else
          Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The Int32 ArrayList contains the following:
     0    2    4    6    8
 The object to search for (3) is not found. The next larger object is at index 2.
 The object to search for (6) is at index 3.
 */ 

    

Return to top


Overloaded Method: BinarySearch(
   object value,
   IComparer comparer
)
Summary
Searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
C# Syntax:
public virtual int BinarySearch(
   object value,
   IComparer comparer
);
Parameters:

value

The Object to locate.

comparer

The IComparer implementation to use when comparing elements.

-or-

null to use the default comparer that is the IComparable implementation of each element.

The IComparer implementation to use when comparing elements.

-or-

null to use the default comparer that is the IComparable implementation of each element.

Return Value:
The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element.
Exceptions
Exception Type Condition
ArgumentException comparer is null and neither value nor the elements of ArrayList implement the IComparable interface.

-or-

comparer is null and value is not of the same type as the elements of the ArrayList.

Remarks
The comparer customizes how the elements are compared. For example, you can use a CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.

If comparer is provided, the elements of the ArrayList are compared to the specified value using the specified IComparer implementation. If the ArrayList is not already sorted according to the sort order defined by comparer, the result might be incorrect.

If comparer is null, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. If the ArrayList is not already sorted according to the IComparable implementation, the result might be incorrect.

Comparing null with any type is allowed and does not generate an exception when using IComparable. When sorting, null is considered to be less than any other object.

If the ArrayList contains more than one element with the same value, the method returns only the index of the first occurrence.

If the ArrayList does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the ArrayList, this index should be used as the insertion point to maintain the sort order.

This method is an O(log n) operation, where n is the number of elements in the range.

Return to top


Overloaded Method: BinarySearch(
   int index,
   int count,
   object value,
   IComparer comparer
)
Summary
Searches a section of the sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
C# Syntax:
public virtual int BinarySearch(
   int index,
   int count,
   object value,
   IComparer comparer
);
Parameters:

index

The zero-based starting index of the range to search.

count

The length of the range to search.

value

The Object to locate.

comparer

The IComparer implementation to use when comparing elements.

-or-

null to use the default comparer that is the IComparable implementation of each element.

The IComparer implementation to use when comparing elements.

-or-

null to use the default comparer that is the IComparable implementation of each element.

Return Value:
The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number, which is the bitwise complement of the index of the next element.
Exceptions
Exception Type Condition
ArgumentException index and count do not denote a valid range in the ArrayList.

-or-

comparer is null and neither value nor the elements of ArrayList implement the IComparable interface.

-or-

comparer is null and value is not of the same type as the elements of the ArrayList.

ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

Remarks
The comparer customizes how the elements are compared. For example, you can use a CaseInsensitiveComparer instance as the comparer to perform case-insensitive string searches.

If comparer is provided, the elements of the ArrayList are compared to the specified value using the specified IComparer implementation. If the ArrayList is not already sorted according to the sort order defined by comparer, the result might be incorrect.

If comparer is null, the comparison is done using the IComparable implementation provided by the element itself or by the specified value. If the ArrayList is not already sorted according to the IComparable implementation, the result might be incorrect.

Comparing null with any type is allowed and does not generate an exception when using IComparable. When sorting, null is considered to be less than any other object.

If the ArrayList contains more than one element with the same value, the method returns only the index of the first occurrence.

If the ArrayList does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operation (~) to this negative integer to get the index of the first element that is larger than the search value. When inserting the value into the ArrayList, this index should be used as the insertion point to maintain the sort order.

This method is an O(log n) operation, where n is the number of elements in the range.

See also:
IComparer | IComparable

Return to top


Method: Clear()
Summary
Removes all elements from the ArrayList.
C# Syntax:
public virtual void Clear();
Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Implements:
IList.Clear
Remarks
ArrayList.Count is set to zero. ArrayList.Capacity remains unchanged. To reset the capacity of the ArrayList, call ArrayList.TrimToSize or set the ArrayList.Capacity property directly. Trimming an empty ArrayList sets the capacity of the ArrayList to the default capacity, not zero.
Example
The following 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( "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 );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 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:
 */ 

    
See also:
ArrayList.TrimToSize | ArrayList.Capacity | ArrayList.Count

Return to top


Method: Clone()
Summary
Creates a shallow copy of the ArrayList.
C# Syntax:
public virtual object Clone();
Return Value:
A shallow copy of the ArrayList.
Implements:
ICloneable.Clone
Remarks
A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new collection point to the same objects that the references in the original collection point to.

In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.

Return to top


Method: Contains(
   object item
)
Summary
Determines whether an element is in the ArrayList.
C# Syntax:
public virtual bool Contains(
   object item
);
Parameters:

item

The Object to locate in the ArrayList.The element to locate can be null. The Object to locate in the ArrayList.The element to locate can be null.

Return Value:
true if item is found in the ArrayList; otherwise, false.
Implements:
IList.Contains
Remarks
This method performs a linear search; therefore, the average execution time is proportional to ArrayList.Count. That is, this method is an O(n) operation, where n is ArrayList.Count.

This method determines equality by calling Object.Equals.

See also:
ArrayList.IndexOf | ArrayList.LastIndexOf

Return to top


Overloaded Method: CopyTo(
   Array array
)
Summary
Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
C# Syntax:
public virtual void CopyTo(
   Array array
);
Parameters:

array

The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.

Exceptions
Exception Type Condition
ArgumentNullException array is null.
ArgumentException array is multidimensional.

-or-

The number of elements in the source ArrayList is greater than the number of elements that the destination array can contain.

InvalidCastException The type of the source ArrayList cannot be cast automatically to the type of the destination array.
Remarks
The specified array must be of a compatible type.

This method uses Array.Copy to copy the elements.

The elements are copied to the Array in the same order in which the enumerator iterates through the ArrayList.

Example
The following example shows how to copy an ArrayList into a one-dimensional Array.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes the source ArrayList.
       ArrayList mySourceList = new ArrayList();
       mySourceList.Add( "three" );
       mySourceList.Add( "napping" );
       mySourceList.Add( "cats" );
       mySourceList.Add( "in" );
       mySourceList.Add( "the" );
       mySourceList.Add( "barn" );
 
       // Creates and initializes the one-dimensional target Array.
       Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
       myTargetArray.SetValue( "The", 0 );
       myTargetArray.SetValue( "quick", 1 );
       myTargetArray.SetValue( "brown", 2 );
       myTargetArray.SetValue( "fox", 3 );
       myTargetArray.SetValue( "jumped", 4 );
       myTargetArray.SetValue( "over", 5 );
       myTargetArray.SetValue( "the", 6 );
       myTargetArray.SetValue( "lazy", 7 );
       myTargetArray.SetValue( "dog", 8 );
 
       // Displays the values of the target Array.
       Console.WriteLine( "The target Array contains the following (before and after copying):" );
       PrintValues( myTargetArray, ' ' );
 
       // Copies the second element from the source ArrayList to the target ArrayList starting at index 7.
       mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source ArrayList to the target ArrayList starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source ArrayList to the target ArrayList starting at index 0.
       mySourceList.CopyTo( myTargetArray );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
    }
 
    public static void PrintValues( Array myArr, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
       int i = 0;
       int cols = myArr.GetLength( myArr.Rank - 1 );
       while ( myEnumerator.MoveNext() )  {
          if ( i < cols )  {
             i++;
          } else  {
             Console.WriteLine();
             i = 1;
          }
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       }
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The target Array contains the following (before and after copying):
  The quick brown fox jumped over the lazy dog      
  The quick brown fox jumped over the napping dog      
  The quick brown fox jumped over three napping cats in the barn   
  three napping cats in the barn three napping cats in the barn
*/ 

    

Return to top


Overloaded Method: CopyTo(
   Array array,
   int arrayIndex
)
Summary
Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
C# Syntax:
public virtual void CopyTo(
   Array array,
   int arrayIndex
);
Parameters:

array

The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.

arrayIndex

The zero-based index in array at which copying begins.

Exceptions
Exception Type Condition
ArgumentNullException array is null.
ArgumentOutOfRangeException arrayIndex is less than zero.
ArgumentException array is multidimensional.

-or-

arrayIndex is equal to or greater than the length of array.

-or-

The number of elements in the source ArrayList is greater than the available space from arrayIndex to the end of the destination array.

InvalidCastException The type of the source ArrayList cannot be cast automatically to the type of the destination array.
Implements:
ICollection.CopyTo
Remarks
The specified array must be of a compatible type.

This method uses Array.Copy to copy the elements.

Example
The following example shows how to copy an ArrayList into a one-dimensional Array.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes the source ArrayList.
       ArrayList mySourceList = new ArrayList();
       mySourceList.Add( "three" );
       mySourceList.Add( "napping" );
       mySourceList.Add( "cats" );
       mySourceList.Add( "in" );
       mySourceList.Add( "the" );
       mySourceList.Add( "barn" );
 
       // Creates and initializes the one-dimensional target Array.
       Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
       myTargetArray.SetValue( "The", 0 );
       myTargetArray.SetValue( "quick", 1 );
       myTargetArray.SetValue( "brown", 2 );
       myTargetArray.SetValue( "fox", 3 );
       myTargetArray.SetValue( "jumped", 4 );
       myTargetArray.SetValue( "over", 5 );
       myTargetArray.SetValue( "the", 6 );
       myTargetArray.SetValue( "lazy", 7 );
       myTargetArray.SetValue( "dog", 8 );
 
       // Displays the values of the target Array.
       Console.WriteLine( "The target Array contains the following (before and after copying):" );
       PrintValues( myTargetArray, ' ' );
 
       // Copies the second element from the source ArrayList to the target ArrayList, starting at index 7.
       mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source ArrayList to the target ArrayList, starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source ArrayList to the target ArrayList, starting at index 0.
       mySourceList.CopyTo( myTargetArray );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
    }
 
    public static void PrintValues( Array myArr, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
       int i = 0;
       int cols = myArr.GetLength( myArr.Rank - 1 );
       while ( myEnumerator.MoveNext() )  {
          if ( i < cols )  {
             i++;
          } else  {
             Console.WriteLine();
             i = 1;
          }
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       }
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The target Array contains the following (before and after copying):
  The quick brown fox jumped over the lazy dog      
  The quick brown fox jumped over the napping dog      
  The quick brown fox jumped over three napping cats in the barn   
  three napping cats in the barn three napping cats in the barn
 */ 

    

Return to top


Overloaded Method: CopyTo(
   int index,
   Array array,
   int arrayIndex,
   int count
)
Summary
Copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
C# Syntax:
public virtual void CopyTo(
   int index,
   Array array,
   int arrayIndex,
   int count
);
Parameters:

index

The zero-based index in the source ArrayList at which copying begins.

array

The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.

arrayIndex

The zero-based index in array at which copying begins.

count

The number of elements to copy.

Exceptions
Exception Type Condition
ArgumentNullException array is null.
ArgumentOutOfRangeException index is less than zero.

-or-

arrayIndex is less than zero.

-or-

count is less than zero.

ArgumentException array is multidimensional.

-or-

index is equal to or greater than the ArrayList.Count of the source ArrayList.

-or-

arrayIndex is equal to or greater than the length of array.

-or-

The number of elements in the source ArrayList is greater than the available space from arrayIndex to the end of the destination array.

InvalidCastException The type of the source ArrayList cannot be cast automatically to the type of the destination array.
Remarks
The specified array must be of a compatible type.
Example
The following example shows how to copy an ArrayList into a one-dimensional Array.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes the source ArrayList.
       ArrayList mySourceList = new ArrayList();
       mySourceList.Add( "three" );
       mySourceList.Add( "napping" );
       mySourceList.Add( "cats" );
       mySourceList.Add( "in" );
       mySourceList.Add( "the" );
       mySourceList.Add( "barn" );
 
       // Creates and initializes the one-dimensional target Array.
       Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
       myTargetArray.SetValue( "The", 0 );
       myTargetArray.SetValue( "quick", 1 );
       myTargetArray.SetValue( "brown", 2 );
       myTargetArray.SetValue( "fox", 3 );
       myTargetArray.SetValue( "jumped", 4 );
       myTargetArray.SetValue( "over", 5 );
       myTargetArray.SetValue( "the", 6 );
       myTargetArray.SetValue( "lazy", 7 );
       myTargetArray.SetValue( "dog", 8 );
 
       // Displays the values of the target Array.
       Console.WriteLine( "The target Array contains the following (before and after copying):" );
       PrintValues( myTargetArray, ' ' );
 
       // Copies the second element from the source ArrayList to the target ArrayList, starting at index 7.
       mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source ArrayList to the target ArrayList, starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source ArrayList to the target ArrayList, starting at index 0.
       mySourceList.CopyTo( myTargetArray );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
    }
 
    public static void PrintValues( Array myArr, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
       int i = 0;
       int cols = myArr.GetLength( myArr.Rank - 1 );
       while ( myEnumerator.MoveNext() )  {
          if ( i < cols )  {
             i++;
          } else  {
             Console.WriteLine();
             i = 1;
          }
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       }
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The target Array contains the following (before and after copying):
  The quick brown fox jumped over the lazy dog      
  The quick brown fox jumped over the napping dog      
  The quick brown fox jumped over three napping cats in the barn   
  three napping cats in the barn three napping cats in the barn
 */ 

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~ArrayList();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: FixedSize(
   ArrayList list
)
Summary
Returns an ArrayList wrapper with a fixed size.
C# Syntax:
public static ArrayList FixedSize(
   ArrayList list
);
Parameters:

list

The ArrayList to wrap.

Return Value:
An ArrayList wrapper with a fixed size.
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
This wrapper can be used to prevent additions to and deletions from the original ArrayList. The elements can still be modified or replaced.
Example
The following example shows how to create a fixed-size wrapper around an 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Create a fixed-size wrapper around the ArrayList.
       ArrayList myFixedSizeAL = ArrayList.FixedSize( myAL );
 
       // Display whether the ArrayLists have a fixed size or not.
       Console.WriteLine( "myAL {0}.", myAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" );
       Console.WriteLine( "myFixedSizeAL {0}.", myFixedSizeAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" );
       Console.WriteLine();
 
       // Display both ArrayLists.
       Console.WriteLine( "Initially," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
 
       // Sort is allowed in the fixed-size ArrayList.
       myFixedSizeAL.Sort();
 
       // Display both ArrayLists.
       Console.WriteLine( "After Sort," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
 
       // Reverse is allowed in the fixed-size ArrayList.
       myFixedSizeAL.Reverse();
 
       // Display both ArrayLists.
       Console.WriteLine( "After Reverse," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
 
       // Add an element to the standard ArrayList.
       myAL.Add( "AddMe" );
 
       // Display both ArrayLists.
       Console.WriteLine( "After adding to the standard ArrayList," );
       Console.Write( "Standard  :" );
       PrintValues( myAL, ' ' );
       Console.Write( "Fixed size:" );
       PrintValues( myFixedSizeAL, ' ' );
       Console.WriteLine();
 
       // Adding or inserting elements to the fixed-size ArrayList throws an exception.
       try  {
          myFixedSizeAL.Add( "AddMe2" );
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
       try  {
          myFixedSizeAL.Insert( 3, "InsertMe" );
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
    }
 
    public static void PrintValues( IEnumerable myList, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.
 
 myAL does not have a fixed size.
 myFixedSizeAL has a fixed size.
 
 Initially,
 Standard  : The quick brown fox jumped over the lazy dog
 Fixed size: The quick brown fox jumped over the lazy dog
 After Sort,
 Standard  : brown dog fox jumped lazy over quick the The
 Fixed size: brown dog fox jumped lazy over quick the The
 After Reverse,
 Standard  : The the quick over lazy jumped fox dog brown
 Fixed size: The the quick over lazy jumped fox dog brown
 After adding to the standard ArrayList,
 Standard  : The the quick over lazy jumped fox dog brown AddMe
 Fixed size: The the quick over lazy jumped fox dog brown AddMe
 
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Add(Object obj)
    at SamplesArrayList.Main()
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
    at SamplesArrayList.Main()
 */ 

    

Return to top


Overloaded Method: FixedSize(
   IList list
)
Summary
Returns an IList wrapper with a fixed size.
C# Syntax:
public static IList FixedSize(
   IList list
);
Parameters:

list

The IList to wrap.

Return Value:
An IList wrapper with a fixed size.
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
This wrapper can be used to prevent additions to and deletions from the original IList. The elements can still be modified or replaced.

Return to top


Overloaded Method: GetEnumerator()
Summary
Returns an enumerator for the entire ArrayList.
C# Syntax:
public virtual IEnumerator GetEnumerator();
Return Value:
An IEnumerator for the entire ArrayList.
Implements:
IEnumerable.GetEnumerator
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.

IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

See also:
IEnumerator

Return to top


Overloaded Method: GetEnumerator(
   int index,
   int count
)
Summary
Returns an enumerator for a section of the ArrayList.
C# Syntax:
public virtual IEnumerator GetEnumerator(
   int index,
   int count
);
Parameters:

index

The zero-based starting index of the ArrayList section that the enumerator should refer to.

count

The number of elements in the ArrayList section that the enumerator should refer to.

Return Value:
An IEnumerator for the specified section of the ArrayList.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

ArgumentException index and count do not specify a valid range in the ArrayList.
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.

IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetRange(
   int index,
   int count
)
Summary
Returns an ArrayList which represents a subset of the elements in the source ArrayList.
C# Syntax:
public virtual ArrayList GetRange(
   int index,
   int count
);
Parameters:

index

The zero-based ArrayList index at which the range starts.

count

The number of elements in the range.

Return Value:
An ArrayList which represents a subset of the elements in the source ArrayList.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

ArgumentException index and count do not denote a valid range of elements in the ArrayList.
Remarks
This method does not create copies of the elements. The new ArrayList is only a view window into the source ArrayList. However, all subsequent changes to the source ArrayList must be done through this view window ArrayList. If changes are made directly to the source ArrayList, the view window ArrayList is invalidated and any operations on it will return an InvalidOperationException.
Example
The following example shows how to set and get a range of elements in 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Creates and initializes the source ICollection.
       Queue mySourceList = new Queue();
       mySourceList.Enqueue( "big" );
       mySourceList.Enqueue( "gray" );
       mySourceList.Enqueue( "wolf" );
 
       // Displays the values of five elements starting at index 0.
       ArrayList mySubAL = myAL.GetRange( 0, 5 );
       Console.WriteLine( "Index 0 through 4 contains:" );
       PrintValues( mySubAL, '\t' );
 
       // Replaces the values of five elements starting at index 1 with the values in the ICollection.
       myAL.SetRange( 1, mySourceList );
 
       // Displays the values of five elements starting at index 0.
       mySubAL = myAL.GetRange( 0, 5 );
       Console.WriteLine( "Index 0 through 4 now contains:" );
       PrintValues( mySubAL, '\t' );
    }
 
    public static void PrintValues( IEnumerable myList, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 Index 0 through 4 contains:
     The    quick    brown    fox    jumped
 Index 0 through 4 now contains:
     The    big    gray    wolf    jumped
 */ 

    
See also:
ArrayList.RemoveRange | ArrayList.AddRange | ArrayList.InsertRange | ArrayList.SetRange

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: IndexOf(
   object value
)
Summary
Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
C# Syntax:
public virtual int IndexOf(
   object value
);
Parameters:

value

The Object to locate in the ArrayList.

Return Value:
The zero-based index of the first occurrence of value within the entire ArrayList, if found; otherwise, -1.
Implements:
IList.IndexOf
Remarks
The ArrayList is searched forward starting at the first element and ending at the last element.

This method performs a linear search. On average, this is an O(n /2) operation, where n is ArrayList.Count. The longest search is an O(n) operation.

This method determines equality by calling Object.Equals.

Example
The following example shows how to determine the index of the first occurrence of a specified element.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList with three elements of the same value.
       ArrayList myAL = new ArrayList();
       myAL.Add( "the" );
       myAL.Add( "quick" );
       myAL.Add( "brown" );
       myAL.Add( "fox" );
       myAL.Add( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
       myAL.Add( "in" );
       myAL.Add( "the" );
       myAL.Add( "barn" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Search for the first occurrence of the duplicated value.
       String myString = "the";
       int myIndex = myAL.IndexOf( myString );
       Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );
 
       // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
       myIndex = myAL.IndexOf( myString, 4 );
       Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );
 
       // Search for the first occurrence of the duplicated value in a section of the ArrayList.
       myIndex = myAL.IndexOf( myString, 6, 6 );
       Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 
 The first occurrence of "the" is at index 0.
 The first occurrence of "the" between index 4 and the end is at index 6.
 The first occurrence of "the" between index 6 and index 11 is at index 6.
 */ 

    
See also:
ArrayList.LastIndexOf | ArrayList.Contains

Return to top


Overloaded Method: IndexOf(
   object value,
   int startIndex
)
Summary
Searches for the specified Object and returns the zero-based index of the first occurrence within the section of the ArrayList that extends from the specified index to the last element.
C# Syntax:
public virtual int IndexOf(
   object value,
   int startIndex
);
Parameters:

value

The Object to locate in the ArrayList.

startIndex

The zero-based starting index of the search.

Return Value:
The zero-based index of the first occurrence of value within the section of the ArrayList that extends from startIndex to the last element, if found; otherwise, -1.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException startIndex is outside the range of valid indexes for the ArrayList.
Remarks
The ArrayList is searched forward starting at startIndex and ending at the last element.

This method performs a linear search. On average, this is an O(n /2) operation, where n is the number of elements from startIndex to the end of the ArrayList. The longest search is an O(n) operation.

This method determines equality by calling Object.Equals.

Example
The following example shows how to determine the index of the first occurrence of a specified element.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList with three elements of the same value.
       ArrayList myAL = new ArrayList();
       myAL.Add( "the" );
       myAL.Add( "quick" );
       myAL.Add( "brown" );
       myAL.Add( "fox" );
       myAL.Add( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
       myAL.Add( "in" );
       myAL.Add( "the" );
       myAL.Add( "barn" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Search for the first occurrence of the duplicated value.
       String myString = "the";
       int myIndex = myAL.IndexOf( myString );
       Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );
 
       // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
       myIndex = myAL.IndexOf( myString, 4 );
       Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );
 
       // Search for the first occurrence of the duplicated value in a section of the ArrayList.
       myIndex = myAL.IndexOf( myString, 6, 6 );
       Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 
 The first occurrence of "the" is at index 0.
 The first occurrence of "the" between index 4 and the end is at index 6.
 The first occurrence of "the" between index 6 and index 11 is at index 6.
 */ 

    
See also:
ArrayList.LastIndexOf | ArrayList.Contains

Return to top


Overloaded Method: IndexOf(
   object value,
   int startIndex,
   int count
)
Summary
Searches for the specified Object and returns the zero-based index of the first occurrence within the section of the ArrayList that starts at the specified index and contains the specified number of elements.
C# Syntax:
public virtual int IndexOf(
   object value,
   int startIndex,
   int count
);
Parameters:

value

The Object to locate in the ArrayList.

startIndex

The zero-based starting index of the search.

count

The number of elements in the section to search.

Return Value:
The zero-based index of the first occurrence of value within the section of the ArrayList that starts at startIndex and contains count number of elements, if found; otherwise, -1.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException startIndex is outside the range of valid indexes for the ArrayList.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in the ArrayList.

Remarks
The ArrayList is searched forward starting at startIndex and ending at startIndex + count - 1.

This method performs a linear search. On average, this is an O(n /2) operation, where n is count. The longest search is an O(n) operation.

This method determines equality by calling Object.Equals.

Example
The following example shows how to determine the index of the first occurrence of a specified element.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList with three elements of the same value.
       ArrayList myAL = new ArrayList();
       myAL.Add( "the" );
       myAL.Add( "quick" );
       myAL.Add( "brown" );
       myAL.Add( "fox" );
       myAL.Add( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
       myAL.Add( "in" );
       myAL.Add( "the" );
       myAL.Add( "barn" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Search for the first occurrence of the duplicated value.
       String myString = "the";
       int myIndex = myAL.IndexOf( myString );
       Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );
 
       // Search for the first occurrence of the duplicated value in the last section of the ArrayList.
       myIndex = myAL.IndexOf( myString, 4 );
       Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );
 
       // Search for the first occurrence of the duplicated value in a section of the ArrayList.
       myIndex = myAL.IndexOf( myString, 6, 6 );
       Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 11 is at index {1}.", myString, myIndex );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 
 The first occurrence of "the" is at index 0.
 The first occurrence of "the" between index 4 and the end is at index 6.
 The first occurrence of "the" between index 6 and index 11 is at index 6.
 */ 

    
See also:
ArrayList.LastIndexOf | ArrayList.Contains

Return to top


Method: Insert(
   int index,
   object value
)
Summary
Inserts an element into the ArrayList at the specified index.
C# Syntax:
public virtual void Insert(
   int index,
   object value
);
Parameters:

index

The zero-based index at which value should be inserted.

value

The Object to insert. The Object to insert.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

index is greater than ArrayList.Count.

NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Implements:
IList.Insert
Remarks
If ArrayList.Count already equals ArrayList.Capacity, the capacity of the list is doubled by automatically reallocating the internal array before the new element is inserted.

If index is equal to ArrayList.Count, value is added to the end of ArrayList.

In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accomodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Example
The following example shows how to insert elements into the ArrayList.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList using Insert instead of Add.
       ArrayList myAL = new ArrayList();
       myAL.Insert( 0, "The" );
       myAL.Insert( 1, "fox" );
       myAL.Insert( 2, "jumped" );
       myAL.Insert( 3, "over" );
       myAL.Insert( 4, "the" );
       myAL.Insert( 5, "dog" );
 
       // Creates and initializes a new Queue.
       Queue myQueue = new Queue();
       myQueue.Enqueue( "quick" );
       myQueue.Enqueue( "brown" );
 
       // Displays the ArrayList and the Queue.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
       Console.WriteLine( "The Queue initially contains the following:" );
       PrintValues( myQueue );
 
       // Copies the Queue elements to the ArrayList at index 1.
       myAL.InsertRange( 1, myQueue );
 
       // Displays the ArrayList.
       Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Search for "dog" and add "lazy" before it.
       myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );
 
       // Displays the ArrayList.
       Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Add "!!!" at the end.
       myAL.Insert( myAL.Count, "!!!" );
 
       // Displays the ArrayList.
       Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Inserting an element beyond Count throws an exception.
       try  {
          myAL.Insert( myAL.Count+1, "anystring" );
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
         The     fox     jumped  over    the     dog
 The Queue initially contains the following:
         quick   brown
 After adding the Queue, the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     dog
 After adding "lazy", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog
 After adding "!!!", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog     !!!
 Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
 Parameter name: index
    at System.Collections.ArrayList.Insert(Int32 index, Object value)
    at SamplesArrayList.Main()
 */ 

    
See also:
ArrayList.InsertRange | ArrayList.Add | ArrayList.Remove

Return to top


Method: InsertRange(
   int index,
   ICollection c
)
Summary
Inserts the elements of a collection into the ArrayList at the specified index.
C# Syntax:
public virtual void InsertRange(
   int index,
   ICollection c
);
Parameters:

index

The zero-based index at which the new elements should be inserted.

c

The ICollection whose elements should be inserted into the ArrayList.

Exceptions
Exception Type Condition
ArgumentNullException c is null.
ArgumentOutOfRangeException index is less than zero.

-or-

index is greater than ArrayList.Count.

NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Remarks
If the new ArrayList.Count (the current ArrayList.Count plus the size of the collection) is greater than ArrayList.Capacity, the capacity of the list is either doubled or increased to the new count, whichever is greater. The internal array is automatically reallocated to accommodate the new elements.

If index is equal to ArrayList.Count, the elements are added to the end of ArrayList.

The order of the elements in the ICollection is preserved in the ArrayList.

In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accomodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Example
The following example shows how to insert elements into the ArrayList.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList using Insert instead of Add.
       ArrayList myAL = new ArrayList();
       myAL.Insert( 0, "The" );
       myAL.Insert( 1, "fox" );
       myAL.Insert( 2, "jumped" );
       myAL.Insert( 3, "over" );
       myAL.Insert( 4, "the" );
       myAL.Insert( 5, "dog" );
 
       // Creates and initializes a new Queue.
       Queue myQueue = new Queue();
       myQueue.Enqueue( "quick" );
       myQueue.Enqueue( "brown" );
 
       // Displays the ArrayList and the Queue.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
       Console.WriteLine( "The Queue initially contains the following:" );
       PrintValues( myQueue );
 
       // Copies the Queue elements to the ArrayList at index 1.
       myAL.InsertRange( 1, myQueue );
 
       // Displays the ArrayList.
       Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Search for "dog" and add "lazy" before it.
       myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );
 
       // Displays the ArrayList.
       Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Add "!!!" at the end.
       myAL.Insert( myAL.Count, "!!!" );
 
       // Displays the ArrayList.
       Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Inserting an element beyond Count throws an exception.
       try  {
          myAL.Insert( myAL.Count+1, "anystring" );
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
         The     fox     jumped  over    the     dog
 The Queue initially contains the following:
         quick   brown
 After adding the Queue, the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     dog
 After adding "lazy", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog
 After adding "!!!", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog     !!!
 Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
 Parameter name: index
    at System.Collections.ArrayList.Insert(Int32 index, Object value)
    at SamplesArrayList.Main()
 */ 

    
See also:
ArrayList.Insert | ArrayList.AddRange | ArrayList.SetRange | ArrayList.GetRange | ArrayList.RemoveRange

Return to top


Overloaded Method: LastIndexOf(
   object value
)
Summary
Searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.
C# Syntax:
public virtual int LastIndexOf(
   object value
);
Parameters:

value

The Object to locate in the ArrayList.

Return Value:
The zero-based index of the last occurrence of value within the entire the ArrayList, if found; otherwise, -1.
Remarks
The ArrayList is searched backward starting at the last element and ending at the first element.

This method performs a linear search. On average, this is an O(n /2) operation, where n is ArrayList.Count. The longest search is an O(n) operation.

Example
The following example shows how to determine the index of the last occurrence of a specified element.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList with three elements of the same value.
       ArrayList myAL = new ArrayList();
       myAL.Add( "the" );
       myAL.Add( "quick" );
       myAL.Add( "brown" );
       myAL.Add( "fox" );
       myAL.Add( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
       myAL.Add( "in" );
       myAL.Add( "the" );
       myAL.Add( "barn" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Searches for the last occurrence of the duplicated value.
       String myString = "the";
       int myIndex = myAL.LastIndexOf( myString );
       Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
 
       // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
       myIndex = myAL.LastIndexOf( myString, 8 );
       Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
 
       // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
       myIndex = myAL.LastIndexOf( myString, 10, 6 );
       Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 is at index {1}.", myString, myIndex );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 10 and index 6 is at index 10.
 */ 

    
See also:
ArrayList.IndexOf | ArrayList.Contains

Return to top


Overloaded Method: LastIndexOf(
   object value,
   int startIndex
)
Summary
Searches for the specified Object and returns the zero-based index of the last occurrence within the section of the ArrayList that extends from the first element to the specified index.
C# Syntax:
public virtual int LastIndexOf(
   object value,
   int startIndex
);
Parameters:

value

The Object to locate in the ArrayList.

startIndex

The zero-based starting index of the backward search.

Return Value:
The zero-based index of the last occurrence of value within the section of the ArrayList that extends from the first element to startIndex, if found; otherwise, -1.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException startIndex is outside the range of valid indexes for the ArrayList.
Remarks
The ArrayList is searched backward starting at startIndex and ending at the first element.

This method performs a linear search. On average, this is an O(n /2) operation, where n is the number of elements from the beginning of the ArrayList to startIndex. The longest search is an O(n) operation.

This method determines equality by calling Object.Equals.

Example
The following example shows how to determine the index of the last occurrence of a specified element.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList with three elements of the same value.
       ArrayList myAL = new ArrayList();
       myAL.Add( "the" );
       myAL.Add( "quick" );
       myAL.Add( "brown" );
       myAL.Add( "fox" );
       myAL.Add( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
       myAL.Add( "in" );
       myAL.Add( "the" );
       myAL.Add( "barn" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Searches for the last occurrence of the duplicated value.
       String myString = "the";
       int myIndex = myAL.LastIndexOf( myString );
       Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
 
       // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
       myIndex = myAL.LastIndexOf( myString, 8 );
       Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
 
       // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
       myIndex = myAL.LastIndexOf( myString, 10, 6 );
       Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 is at index {1}.", myString, myIndex );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 10 and index 6 is at index 10.
 */ 

    
See also:
ArrayList.IndexOf | ArrayList.Contains

Return to top


Overloaded Method: LastIndexOf(
   object value,
   int startIndex,
   int count
)
Summary
Searches for the specified Object and returns the zero-based index of the last occurrence within the section of the ArrayList that contains the specified number of elements and ends at the specified index.
C# Syntax:
public virtual int LastIndexOf(
   object value,
   int startIndex,
   int count
);
Parameters:

value

The Object to locate in the ArrayList.

startIndex

The zero-based starting index of the backward search.

count

The number of elements in the section to search.

Return Value:
The zero-based index of the last occurrence of value within the section of the ArrayList that contains count number of elements and ends at startIndex, if found; otherwise, -1.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException startIndex is outside the range of valid indexes for the ArrayList.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in the ArrayList.

Remarks
The ArrayList is searched backward starting at startIndex and ending at startIndex - count + 1.

This method performs a linear search. On average, this is an O(n /2) operation, where n is count. The longest search is an O(n) operation.

This method determines equality by calling Object.Equals.

Example
The following example shows how to determine the index of the last occurrence of a specified element. Note that LastIndexOf is a backward search; therefore, count must be less than or equal to startIndex + 1.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList with three elements of the same value.
       ArrayList myAL = new ArrayList();
       myAL.Add( "the" );
       myAL.Add( "quick" );
       myAL.Add( "brown" );
       myAL.Add( "fox" );
       myAL.Add( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
       myAL.Add( "in" );
       myAL.Add( "the" );
       myAL.Add( "barn" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Searches for the last occurrence of the duplicated value.
       String myString = "the";
       int myIndex = myAL.LastIndexOf( myString );
       Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
 
       // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
       myIndex = myAL.LastIndexOf( myString, 8 );
       Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
 
       // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
       myIndex = myAL.LastIndexOf( myString, 10, 6 );
       Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 is at index {1}.", myString, myIndex );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
     [9]:    in
     [10]:    the
     [11]:    barn
 
 The last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 10 and index 6 is at index 10.
 */ 

    
See also:
ArrayList.IndexOf | ArrayList.Contains

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: ReadOnly(
   ArrayList list
)
Summary
Returns a read-only ArrayList wrapper.
C# Syntax:
public static ArrayList ReadOnly(
   ArrayList list
);
Parameters:

list

The ArrayList to wrap.

Return Value:
A read-only ArrayList wrapper around list.
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
To prevent any modifications to list, expose list only through this wrapper.
Example
The following example shows how to create a read-only wrapper around an ArrayList and how to determine if an ArrayList is read-only.
 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("!");
 
       // Creates a read-only copy of the ArrayList.
       ArrayList myReadOnlyAL = ArrayList.ReadOnly( myAL );
 
       // Displays whether the ArrayList is read-only or writable.
       Console.WriteLine( "myAL is {0}.", myAL.IsReadOnly ? "read only" : "writable" );
       Console.WriteLine( "myReadOnlyAL is {0}.", myReadOnlyAL.IsReadOnly ? "read only" : "writable" );
 
       // Adding an element to a read-only ArrayList throws an exception.
       try  {
          myReadOnlyAL.Add("Boo!");
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }
    }
 }
 /* 
 This code produces the following output.
 
 myAL is writable.
 myReadOnlyAL is read only.
 Exception: System.NotSupportedException: Collection is read-only.
    at System.Collections.ReadOnlyArrayList.Add(Object obj)
    at SamplesArrayList.Main()
 */

    
See also:
ArrayList.IsReadOnly

Return to top


Overloaded Method: ReadOnly(
   IList list
)
Summary
Returns a read-only IList wrapper.
C# Syntax:
public static IList ReadOnly(
   IList list
);
Parameters:

list

The IList to wrap.

Return Value:
A read-only IList wrapper around list.
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
To prevent any modifications to list, expose list only through this wrapper.
See also:
ArrayList.IsReadOnly

Return to top


Method: Remove(
   object obj
)
Summary
Removes the first occurrence of a specific object from the ArrayList.
C# Syntax:
public virtual void Remove(
   object obj
);
Parameters:

obj

The Object to remove from the ArrayList.

Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Implements:
IList.Remove
Remarks
This method performs a linear search; therefore, the average execution time is proportional to ArrayList.Count. That is, this method is an O(n) operation, where n is ArrayList.Count.

This method determines equality by calling Object.Equals.

In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Example
The following example shows how to remove elements from 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
 
       // Removes the element containing "lazy".
       myAL.Remove( "lazy" );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing \"lazy\":" );
       PrintValues( myAL );
 
       // Removes the element at index 5.
       myAL.RemoveAt( 5 );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing the element at index 5:" );
       PrintValues( myAL );
 
       // Removes three elements starting at index 4.
       myAL.RemoveRange( 4, 3 );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing three elements starting at index 4:" );
       PrintValues( myAL );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 After removing "lazy":
     The    quick    brown    fox    jumped    over    the    dog
 After removing the element at index 5:
     The    quick    brown    fox    jumped    the    dog
 After removing three elements starting at index 4:
     The    quick    brown    fox
 */ 

    
See also:
ArrayList.RemoveAt | ArrayList.RemoveRange | ArrayList.Add | ArrayList.Insert

Return to top


Method: RemoveAt(
   int index
)
Summary
Removes the element at the specified index of the ArrayList.
C# Syntax:
public virtual void RemoveAt(
   int index
);
Parameters:

index

The zero-based index of the element to remove.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

index is equal to or greater than ArrayList.Count.

NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Implements:
IList.RemoveAt
Remarks
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
Example
The following example shows how to remove elements from 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
 
       // Removes the element containing "lazy".
       myAL.Remove( "lazy" );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing \"lazy\":" );
       PrintValues( myAL );
 
       // Removes the element at index 5.
       myAL.RemoveAt( 5 );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing the element at index 5:" );
       PrintValues( myAL );
 
       // Removes three elements starting at index 4.
       myAL.RemoveRange( 4, 3 );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing three elements starting at index 4:" );
       PrintValues( myAL );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 After removing "lazy":
     The    quick    brown    fox    jumped    over    the    dog
 After removing the element at index 5:
     The    quick    brown    fox    jumped    the    dog
 After removing three elements starting at index 4:
     The    quick    brown    fox
 */ 

    
See also:
ArrayList.Remove | ArrayList.RemoveRange | ArrayList.Add | ArrayList.Insert

Return to top


Method: RemoveRange(
   int index,
   int count
)
Summary
Removes a range of elements from the ArrayList.
C# Syntax:
public virtual void RemoveRange(
   int index,
   int count
);
Parameters:

index

The zero-based starting index of the range of elements to remove.

count

The number of elements to remove.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

ArgumentException index and count do not denote a valid range of elements in the ArrayList.
NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Remarks
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
Example
The following example shows how to remove elements from 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
 
       // Removes the element containing "lazy".
       myAL.Remove( "lazy" );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing \"lazy\":" );
       PrintValues( myAL );
 
       // Removes the element at index 5.
       myAL.RemoveAt( 5 );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing the element at index 5:" );
       PrintValues( myAL );
 
       // Removes three elements starting at index 4.
       myAL.RemoveRange( 4, 3 );
 
       // Displays the current state of the ArrayList.
       Console.WriteLine( "After removing three elements starting at index 4:" );
       PrintValues( myAL );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 After removing "lazy":
     The    quick    brown    fox    jumped    over    the    dog
 After removing the element at index 5:
     The    quick    brown    fox    jumped    the    dog
 After removing three elements starting at index 4:
     The    quick    brown    fox
 */ 

    
See also:
ArrayList.Remove | ArrayList.RemoveAt | ArrayList.GetRange | ArrayList.AddRange | ArrayList.InsertRange | ArrayList.SetRange

Return to top


Method: Repeat(
   object value,
   int count
)
Summary
Returns an ArrayList whose elements are copies of the specified value.
C# Syntax:
public static ArrayList Repeat(
   object value,
   int count
);
Parameters:

value

The Object to copy multiple times in the new ArrayList. The value to copy can be null. The Object to copy multiple times in the new ArrayList. The value to copy can be null.

count

The number of times value should be copied.

Return Value:
An ArrayList with count number of elements, all of which are copies of value.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException count is less than zero.
Example
The following example shows how to create and initialize a new ArrayList with the same value.
 using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates a new ArrayList with five elements and initialize each element with a null value.
       ArrayList myAL = ArrayList.Repeat( null, 5 );
 
       // Displays the count, capacity and values of the ArrayList.
       Console.WriteLine( "ArrayList with five elements with a null value" );
       Console.WriteLine( "   Count    : {0}", myAL.Count );
       Console.WriteLine( "   Capacity : {0}", myAL.Capacity );
       Console.Write( "   Values:" );
       PrintValues( myAL );
 
       // Creates a new ArrayList with seven elements and initialize each element with the string "abc".
       myAL = ArrayList.Repeat( "abc", 7 );
 
       // Displays the count, capacity and values of the ArrayList.
       Console.WriteLine( "ArrayList with seven elements with a string value" );
       Console.WriteLine( "   Count    : {0}", myAL.Count );
       Console.WriteLine( "   Capacity : {0}", myAL.Capacity );
       Console.Write( "   Values:" );
       PrintValues( myAL );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 ArrayList with five elements with a null value
    Count    : 5
    Capacity : 16
    Values:					
 ArrayList with seven elements with a string value
    Count    : 7
    Capacity : 16
    Values:	abc	abc	abc	abc	abc	abc	abc
 */ 

    

Return to top


Overloaded Method: Reverse()
Summary
Reverses the order of the elements in the entire ArrayList.
C# Syntax:
public virtual void Reverse();
Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.
Example
The following example shows how to reverse the sort order of the values in an 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Reverses the sort order of the values of the ArrayList.
       myAL.Reverse();
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "After reversing:" );
       PrintIndexAndValues( myAL );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
     [0]:    The
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 
 After reversing:
     [0]:    dog
     [1]:    lazy
     [2]:    the
     [3]:    over
     [4]:    jumped
     [5]:    fox
     [6]:    brown
     [7]:    quick
     [8]:    The
 */ 

    

Return to top


Overloaded Method: Reverse(
   int index,
   int count
)
Summary
Reverses the order of the elements in the specified range.
C# Syntax:
public virtual void Reverse(
   int index,
   int count
);
Parameters:

index

The zero-based starting index of the range to reverse.

count

The number of elements in the range to reverse.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

ArgumentException index and count do not denote a valid range of elements in the ArrayList.
NotSupportedException The ArrayList is read-only.
Remarks
This method uses Array.Reverse to reverse the order of the elements, such that the element at ArrayList [i], where i is any index within the range, moves to ArrayList [j], where j equals index + index + count - i - 1.
Example
The following example shows how to reverse the sort order of the values in a section of an 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Reverses the sort order of the values of the ArrayList.
       myAL.Reverse( 1, 3 );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "After reversing:" );
       PrintIndexAndValues( myAL );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
     [0]:    The
     [1]:    QUICK
     [2]:    BROWN
     [3]:    FOX
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 
 After reversing:
     [0]:    The
     [1]:    FOX
     [2]:    BROWN
     [3]:    QUICK
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 */ 

    

Return to top


Method: SetRange(
   int index,
   ICollection c
)
Summary
Copies the elements of a collection over a range of elements in the ArrayList.
C# Syntax:
public virtual void SetRange(
   int index,
   ICollection c
);
Parameters:

index

The zero-based ArrayList index at which to start copying the elements of c.

c

The ICollection whose elements to copy to the ArrayList.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

index plus the number of elements in c is greater than ArrayList.Count.

ArgumentNullException c is null.
NotSupportedException The ArrayList is read-only.
Example
The following example shows how to set and get a range of elements in 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Creates and initializes the source ICollection.
       Queue mySourceList = new Queue();
       mySourceList.Enqueue( "big" );
       mySourceList.Enqueue( "gray" );
       mySourceList.Enqueue( "wolf" );
 
       // Displays the values of five elements starting at index 0.
       ArrayList mySubAL = myAL.GetRange( 0, 5 );
       Console.WriteLine( "Index 0 through 4 contains:" );
       PrintValues( mySubAL, '\t' );
 
       // Replaces the values of five elements starting at index 1 with the values in the ICollection.
       myAL.SetRange( 1, mySourceList );
 
       // Displays the values of five elements starting at index 0.
       mySubAL = myAL.GetRange( 0, 5 );
       Console.WriteLine( "Index 0 through 4 now contains:" );
       PrintValues( mySubAL, '\t' );
    }
 
    public static void PrintValues( IEnumerable myList, char mySeparator )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 Index 0 through 4 contains:
     The    quick    brown    fox    jumped
 Index 0 through 4 now contains:
     The    big    gray    wolf    jumped
 */ 

    
See also:
ArrayList.AddRange | ArrayList.InsertRange | ArrayList.GetRange | ArrayList.RemoveRange

Return to top


Overloaded Method: Sort()
Summary
Sorts the elements in the entire ArrayList using the IComparable implementation of each element.
C# Syntax:
public virtual void Sort();
Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.
Remarks
This method uses Array.Sort, which uses the QuickSort algorithm. This is an O(n log n) operation, where n is the number of elements to sort.
Example
The following example shows how to sort the values in an 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Sorts the values of the ArrayList.
       myAL.Sort();
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "After sorting:" );
       PrintIndexAndValues( myAL );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
     [0]:    The
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 
 After sorting:
     [0]:    brown
     [1]:    dog
     [2]:    fox
     [3]:    jumped
     [4]:    lazy
     [5]:    over
     [6]:    quick
     [7]:    the
     [8]:    The
 */

    

Return to top


Overloaded Method: Sort(
   IComparer comparer
)
Summary
Sorts the elements in the entire ArrayList using the specified comparer.
C# Syntax:
public virtual void Sort(
   IComparer comparer
);
Parameters:

comparer

The IComparer implementation to use when comparing elements.

-or-

null to use the IComparable implementation of each element.

The IComparer implementation to use when comparing elements.

-or-

null to use the IComparable implementation of each element.

Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.
Remarks
This method uses Array.Sort, which uses the QuickSort algorithm. This is an O(n log n) operation, where n is the number of elements to sort.

Return to top


Overloaded Method: Sort(
   int index,
   int count,
   IComparer comparer
)
Summary
Sorts the elements in a section of ArrayList using the specified comparer.
C# Syntax:
public virtual void Sort(
   int index,
   int count,
   IComparer comparer
);
Parameters:

index

The zero-based starting index of the range to sort.

count

The length of the range to sort.

comparer

The IComparer implementation to use when comparing elements. -or-

null to use the IComparable implementation of each element.

-or-

null to use the IComparable implementation of each element.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

ArgumentException index and count do not specify a valid range in the ArrayList.
NotSupportedException The ArrayList is read-only.
Remarks
This method uses Array.Sort, which uses the QuickSort algorithm. This is an O(n log n) operation, where n is the number of elements to sort.
Example
The following example shows how to sort the values in a section of an 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( "jumped" );
       myAL.Add( "over" );
       myAL.Add( "the" );
       myAL.Add( "lazy" );
       myAL.Add( "dog" );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "The ArrayList initially contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Sorts the values of the ArrayList.
       myAL.Sort( 1, 3, null );
 
       // Displays the values of the ArrayList.
       Console.WriteLine( "After sorting:" );
       PrintIndexAndValues( myAL );
    }
 
    public static void PrintIndexAndValues( IEnumerable myList )  {
       int i = 0;
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
     [0]:    The
     [1]:    QUICK
     [2]:    BROWN
     [3]:    FOX
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 
 After sorting:
     [0]:    The
     [1]:    BROWN
     [2]:    FOX
     [3]:    QUICK
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 */ 

    

Return to top


Overloaded Method: Synchronized(
   ArrayList list
)
Summary
Returns an ArrayList wrapper that is synchronized (thread-safe).
C# Syntax:
public static ArrayList Synchronized(
   ArrayList list
);
Parameters:

list

The ArrayList to synchronize.

Return Value:
An ArrayList wrapper that is synchronized (thread-safe).
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
To guarantee the thread safety of the ArrayList, all operations must be done through this wrapper.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

The following code example shows how to lock the collection using the ArrayList.SyncRoot during the entire enumeration:

              ArrayList myCollection = new ArrayList();
               lock( myCollection.SyncRoot ) {
               foreach ( Object item in myCollection ) {
               // Insert your code here.
               }
              }
            
Example
The following example shows how to synchronize an ArrayList, determine if an ArrayList is synchronized and use a synchronized 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" );
 
       // Creates a synchronized wrapper around the ArrayList.
       ArrayList mySyncdAL = ArrayList.Synchronized( myAL );
 
       // Displays the sychronization status of both ArrayLists.
       Console.WriteLine( "myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized" );
       Console.WriteLine( "mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized" );
    }
 }
 /* 
 This code produces the following output.
 
 myAL is not synchronized.
 mySyncdAL is synchronized.
 */ 

    
See also:
ArrayList.SyncRoot | ArrayList.IsSynchronized

Return to top


Overloaded Method: Synchronized(
   IList list
)
Summary
Returns an IList wrapper that is synchronized (thread-safe).
C# Syntax:
public static IList Synchronized(
   IList list
);
Parameters:

list

The IList to synchronize.

Return Value:
An IList wrapper that is synchronized (thread-safe).
Exceptions
Exception Type Condition
ArgumentNullException list is null.
Remarks
To guarantee the thread safety of the ArrayList, all operations must be done through this wrapper.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

The following code example shows how to lock the collection using the ArrayList.SyncRoot during the entire enumeration:

              ArrayList myCollection = new ArrayList();
               lock( myCollection.SyncRoot ) {
               foreach ( Object item in myCollection ) {
               // Insert your code here.
               }
              }
            
See also:
ArrayList.SyncRoot | ArrayList.IsSynchronized

Return to top


Overloaded Method: ToArray()
Summary
Copies the elements of the ArrayList to a new Object array.
C# Syntax:
public virtual object[] ToArray();
Return Value:
An Object array containing copies of the elements of the ArrayList.
Remarks
The elements are copied using Array.Copy, which is an O(n) operation, where n is ArrayList.Count.

Return to top


Overloaded Method: ToArray(
   Type type
)
Summary
Copies the elements of the ArrayList to a new array of the specified type.
C# Syntax:
public virtual Array ToArray(
   Type type
);
Parameters:

type

The Type of array to create and copy elements to.

Return Value:
An array of the specified type containing copies of the elements of the ArrayList.
Exceptions
Exception Type Condition
ArgumentNullException type is null.
InvalidCastException The type of the source ArrayList cannot be cast automatically to the specified type.
Remarks
The elements are copied using Array.Copy, which is an O(n) operation, where n is ArrayList.Count.

Elements can be cast to the specified type as required. If the casting cannot be done automatically, the copy operation might fail.

See also:
Type

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: TrimToSize()
Summary
Sets the capacity to the actual number of elements in the ArrayList.
C# Syntax:
public virtual void TrimToSize();
Exceptions
Exception Type Condition
NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Remarks
This method can be used to minimize a list's memory overhead if no new elements will be added to the list.

To completely clear all elements in a list, call the ArrayList.Clear method before calling ArrayList.TrimToSize. Trimming an empty ArrayList sets the capacity of the ArrayList to the default capacity, not zero.

Example
The following 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( "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 );
    }
 
    public static void PrintValues( IEnumerable myList )  {
       System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
       while ( myEnumerator.MoveNext() )
          Console.Write( "\t{0}", myEnumerator.Current );
       Console.WriteLine();
    }
 }
 /* 
 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:
 */ 

    
See also:
ArrayList.Clear | ArrayList.Capacity | ArrayList.Count

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.