HomeSearch

VB.NET Array.Resize Subroutine

Use the Array.Resize subroutine to grow and shrink an array.
Array.Resize. An array can be resized. This will either remove elements at the end if you resize it to be smaller, or add elements at the end if you resize it to be bigger. We show how the Array.Resize subroutine works in the VB.NET language.Array
Example. This program first declares an array of integers with three elements. Then, it displays the elements and also the array's total length. Next, it invokes the Array.Resize subroutine.

And: This makes the new length of the array 6. There are now three elements at the end that were not there before.

For Each, For
VB.NET program that uses Array.Resize Module Module1 Sub Main() ' Create array of three elements. Dim arr(2) As Integer arr(0) = 1 arr(1) = 2 arr(2) = 3 ' Display all elements and the length. For Each value As Integer In arr Console.Write(value) Console.Write(" ") Next Console.Write("- ") Console.WriteLine(arr.Length) ' Call Array.Resize. Array.Resize(arr, 6) ' Display all elements and the length. For Each value As Integer In arr Console.Write(value) Console.Write(" ") Next Console.Write("- ") Console.WriteLine(arr.Length) End Sub End Module Output 1 2 3 - 3 1 2 3 0 0 0 - 6
Why are the elements zero? The default value of an integer on the heap is the value zero. All the bits are set to zeros. For strings the default value will be Nothing, because no objects were allocated for the new references.Nothing
Summary. The Array.Resize subroutine is used in almost all collections in the .NET Framework. When a collection such as a List or Dictionary needs to become larger, it calls Array.Resize to resize its internal buffers, which are arrays.ListDictionary

Note: You can use Array.Resize to develop your own resizable collections as well.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls