Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,845 questions

55,674 answers

573 users

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

1 Answer

0 votes
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
' 

 



answered Aug 17 by avibootz
...