C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: With an Object array, a Function can accept any number of arguments—of any type.
Main: An Object array of 5 elements is declared. Each of the 5 elements are assigned to an Object.
Info: The elements in the Object array are all of type Object, but they also have more derived types, including StringBuilder, String, and Int32.
StringBuilderStringsIntegerHowever: You can still reference these types as Object types. They have their derived type, and their base types.
VB.NET program that creates Object array
Imports System.Text
Module Module1
Sub Main()
' Create an array of five elements.
Dim arr(4) As Object
arr(0) = New Object()
arr(1) = New StringBuilder("Initialized")
arr(2) = "Literal"
arr(3) = 6
arr(4) = Nothing
' Pass array to Subroutine.
Write(arr)
End Sub
Sub Write(ByVal arr() As Object)
' Loop over each element.
For Each element As Object In arr
' Avoid Nothing elements.
If element IsNot Nothing Then
Console.WriteLine(element)
Console.WriteLine(element.GetType())
Console.WriteLine("---")
End If
Next
End Sub
End Module
Output
System.Object
System.Object
---
Initialized
System.Text.StringBuilder
---
Literal
System.String
---
6
System.Int32
---
Tip: This gives you great flexibility at the cost of performance, because elements must then be cast or checked again to be used.