C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
VB.NET program that uses ReDim
Module Module1
    Sub Main()
        ' Create an array with 3 Integer elements.
        Dim x() As Integer = New Integer() {1, 2, 3}
        Console.WriteLine(String.Join(",", x))
        ' Use ReDim to resize the array.
        ReDim x(5)
        Console.WriteLine(String.Join(",", x))
        ' Use ReDim to shorten the array.
        ReDim x(2)
        Console.WriteLine(String.Join(",", x))
    End Sub
End Module
Output
1,2,3
0,0,0,0,0,0
0,0,0
VB.NET program that uses Array.Resize
Module Module1
    Sub Main()
        Dim x() As Integer = New Integer() {1, 2, 3}
        ' This keeps the existing elements.
        Array.Resize(x, 6)
        Console.WriteLine(String.Join(",", x))
    End Sub
End Module
Output
1,2,3,0,0,0
Version 1: This version uses ReDim to resize the array to have 6 elements (it specifies 5 as the last index).
Version 2: This version uses Array.Resize. It retains the existing elements, unlike ReDim.
Result: ReDim is faster than Array.Resize. So if possible, ReDim is a better option than Array.Resize.
VB.NET program that benchmarks ReDim, Array.Resize
Module Module1
    Sub Main()
        Dim m As Integer = 10000000
        Dim s1 As Stopwatch = Stopwatch.StartNew
        ' Version 1: use ReDim.
        For i As Integer = 0 To m - 1
            Dim x() As Integer = New Integer() {1, 2, 3}
            ReDim x(5)
            If Not x.Length = 6 Then
                Return
            End If
        Next
        s1.Stop()
        Dim s2 As Stopwatch = Stopwatch.StartNew
        ' Version 2: use Array.Resize.
        For i As Integer = 0 To m - 1
            Dim x() As Integer = New Integer() {1, 2, 3}
            Array.Resize(x, 6)
            If Not x.Length = 6 Then
                Return
            End If
        Next
        s2.Stop()
        Dim u As Integer = 1000000
        Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
        Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
    End Sub
End Module
Output
10.70 ns    ReDim
70.40 ns    Array.Resize