How to initialize an array with a range of numbers in VB.NET

1 Answer

0 votes
Imports System
Imports System.Linq

Module RangeArrayInitializer

    Sub Main()
        Dim startNum As Integer = 1
        Dim endNum As Integer = 10

        ' 1. Standard LINQ Enumerable.Range approach
        Dim linqArray As Integer() = CreateRangeWithLinq(startNum, endNum)
        Console.WriteLine("LINQ Range [1, 10]: " & String.Join(", ", linqArray))

        ' 2. High-performance imperative approach using a pre-allocated array
        Dim loopArray As Integer() = CreateRangeWithLoop(startNum, endNum)
        Console.WriteLine("Loop Range [1, 10]: " & String.Join(", ", loopArray))

        ' 3. Flexible method supporting non-unit step increments
        Dim stepArray As Integer() = CreateRangeWithStep(1, 10, 2)
        Console.WriteLine("Step Range [1, 10] step 2: " & String.Join(", ", stepArray))
    End Sub

    ''' <summary>
    ''' Generates an array of integers representing a sequential range [start, endInclusive].
    ''' Uses Enumerable.Range to express the intent cleanly in a functional style.
    ''' </summary>
    ''' <param name="startNum">Starting integer of the range</param>
    ''' <param name="endInclusive">Ending integer of the range (inclusive)</param>
    ''' <returns>An array filled with sequential values</returns>
    Public Function CreateRangeWithLinq(startNum As Integer, endInclusive As Integer) As Integer()
        ' Enumerable.Range requires starting value and total count (length)
        Dim count As Integer = (endInclusive - startNum) + 1

        ' Generate sequence and immediately materialize it into a 0-indexed primitive array
        Return Enumerable.Range(startNum, count).ToArray()
    End Function

    ''' <summary>
    ''' Generates an integer array using a pre-allocated loop.
    ''' Delivers low overhead and optimal execution speed.
    ''' </summary>
    ''' <param name="startNum">Starting integer of the range</param>
    ''' <param name="endInclusive">Ending integer of the range (inclusive)</param>
    ''' <returns>An array populated sequentially</returns>
    Public Function CreateRangeWithLoop(startNum As Integer, endInclusive As Integer) As Integer()
        Dim count As Integer = (endInclusive - startNum) + 1
        
        ' Allocate array with precise size (VB upper bound is count - 1)
        Dim result(count - 1) As Integer

        ' Populates elements directly without resizing overhead
        For i As Integer = 0 To count - 1
            result(i) = startNum + i
        Next

        Return result
    End Function

    ''' <summary>
    ''' Creates an array with custom step intervals using Enumerable.Range.
    ''' </summary>
    ''' <param name="startNum">Starting integer</param>
    ''' <param name="endInclusive">Maximum threshold bound</param>
    ''' <param name="stepValue">Increment between sequential numbers</param>
    ''' <returns>An array of numbers incremented by the specified step</returns>
    Public Function CreateRangeWithStep(startNum As Integer, endInclusive As Integer, stepValue As Integer) As Integer()
        Dim count As Integer = ((endInclusive - startNum) \ stepValue) + 1

        ' Select projects index multiplication to handle custom step sizes cleanly
        Return Enumerable.Range(0, count).
            Select(Function(i) startNum + (i * stepValue)).
            ToArray()
    End Function

End Module


	
' run:
'
' LINQ Range [1, 10]: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
' Loop Range [1, 10]: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
' Step Range [1, 10] step 2: 1, 3, 5, 7, 9
'

 



answered Aug 17 by avibootz
...