Imports System
Imports System.Collections.Generic
Imports System.Linq
Module RangeListInitializer
Sub Main()
Dim startNum As Integer = 1
Dim endNum As Integer = 10
' 1. Expressive LINQ approach converting Enumerable.Range directly to a List
Dim linqList As List(Of Integer) = CreateRangeListWithLinq(startNum, endNum)
Console.WriteLine("LINQ List [1, 10]: " & String.Join(", ", linqList))
' 2. Pre-allocated List constructor approach (High performance / Low memory overhead)
Dim loopList As List(Of Integer) = CreateRangeListWithLoop(startNum, endNum)
Console.WriteLine("Loop List [1, 10]: " & String.Join(", ", loopList))
' 3. Custom step sequence populated into a List
Dim stepList As List(Of Integer) = CreateRangeListWithStep(1, 10, 2)
Console.WriteLine("Step List [1, 10] step 2: " & String.Join(", ", stepList))
End Sub
''' <summary>
''' Generates a List(Of Integer) containing a sequential range [startNum, endInclusive].
''' Uses Enumerable.Range for a clean, functional style.
''' </summary>
''' <param name="startNum">Starting integer of the range</param>
''' <param name="endInclusive">Ending integer of the range (inclusive)</param>
''' <returns>A List populated with sequential integers</returns>
Public Function CreateRangeListWithLinq(startNum As Integer, endInclusive As Integer) As List(Of Integer)
' Enumerable.Range takes the starting value and total item count
Dim count As Integer = (endInclusive - startNum) + 1
' ToList materializes the sequence into a mutable List(Of Integer)
Return Enumerable.Range(startNum, count).ToList()
End Function
''' <summary>
''' Generates a List(Of Integer) using an explicit capacity allocation and loop.
''' Pre-sizing capacity prevents dynamic array re-allocations during growth.
''' </summary>
''' <param name="startNum">Starting integer of the range</param>
''' <param name="endInclusive">Ending integer of the range (inclusive)</param>
''' <returns>A pre-sized List populated sequentially</returns>
Public Function CreateRangeListWithLoop(startNum As Integer, endInclusive As Integer) As List(Of Integer)
Dim count As Integer = (endInclusive - startNum) + 1
' Pre-allocate list capacity to exact count for efficient memory usage
Dim result As New List(Of Integer)(count)
For current As Integer = startNum To endInclusive
result.Add(current)
Next
Return result
End Function
''' <summary>
''' Creates a List(Of Integer) supporting custom step intervals.
''' </summary>
''' <param name="startNum">Starting integer</param>
''' <param name="endInclusive">Maximum threshold bound</param>
''' <param name="stepValue">Increment between sequential numbers</param>
''' <returns>A List of numbers incremented by the specified step</returns>
Public Function CreateRangeListWithStep(startNum As Integer, endInclusive As Integer, stepValue As Integer) As List(Of Integer)
Dim count As Integer = ((endInclusive - startNum) \ stepValue) + 1
' Projects indices using the step interval and returns a List
Return Enumerable.Range(0, count).
Select(Function(i) startNum + (i * stepValue)).
ToList()
End Function
End Module
' run:
'
' LINQ List [1, 10]: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
' Loop List [1, 10]: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
' Step List [1, 10] step 2: 1, 3, 5, 7, 9
'