ArrayList.ToArray Method (Type)

Copies the elements of the ArrayList to a new array of the specified element type.

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

'Declaration
Public Overridable Function ToArray ( _
	type As Type _
) As Array

Parameters

type
Type: System.Type

The element Type of the destination array to create and copy elements to.

Return Value

Type: System.Array
An array of the specified element type containing copies of the elements of the ArrayList.

ExceptionCondition
ArgumentNullException

type is Nothing.

InvalidCastException

The type of the source ArrayList cannot be cast automatically to the specified type.

All of the objects in the ArrayList object will be cast to the Type specified in the type parameter.

The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.

The following copy example shows how to copy the elements of an ArrayList to a string array.

Imports System
Imports System.Collections

Public Class SamplesArrayList

    Public Shared Sub Main()

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

        ' Displays the values of the ArrayList.
        Console.WriteLine("The ArrayList contains the following values:")
        PrintIndexAndValues(myAL)

        ' Copies the elements of the ArrayList to a string array. 
        Dim myArr As String() = CType(myAL.ToArray(GetType(String)), String())

        ' Displays the contents of the string array.
        Console.WriteLine("The string array contains the following values:")
        PrintIndexAndValues(myArr)

    End Sub 'Main

    Overloads Public Shared Sub PrintIndexAndValues(myList As ArrayList)
        Dim i As Integer = 0
        Dim o As [Object]
        For Each o In  myList
            Console.WriteLine("        [{0}]:    {1}", i, o)
            i = i + 1
        Next o
        Console.WriteLine()
    End Sub 'PrintIndexAndValues

    Overloads Public Shared Sub PrintIndexAndValues(myArr() As String)
        Dim i As Integer 
        For i = 0 To myArr.Length - 1
            Console.WriteLine("        [{0}]:    {1}", i, myArr(i))
        Next i
        Console.WriteLine()
    End Sub 'PrintIndexAndValues

End Class 'SamplesArrayList


'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 

'The string array contains the following values: 
'        [0]:    The 
'        [1]:    quick 
'        [2]:    brown 
'        [3]:    fox 
'        [4]:    jumped 
'        [5]:    over 
'        [6]:    the 
'        [7]:    lazy 
'        [8]:    dog

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft