C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
However: When we loop through the Object array, we can cast each element to a String.
Object ArrayNote: We can not directly cast the array to a String(). This exception is reported.
Quote: Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'System.String[]'.
VB.NET program that converts ArrayList to Object array
Module Module1
Sub Main()
' Create ArrayList.
Dim list As ArrayList = New ArrayList()
list.Add("Dot")
list.Add("Net")
list.Add("Perls")
' Get array of objects.
Dim array() As Object = list.ToArray()
For Each element In array
' Cast object to string.
Dim value As String = element
Console.WriteLine(value)
Next
End Sub
End Module
Output
Dot
Net
Perls