VB.NET
list initialization
programming
duplicates
inline coding

Inline list initialization in VB.NET

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In VB.NET, inline list initialization uses collection initializer syntax with the From keyword. It is a concise way to create a List(Of T) with starting values in one expression instead of constructing the list first and then calling Add repeatedly.

Basic List(Of T) Initialization

The most common form looks like this:

vbnet
1Imports System
2Imports System.Collections.Generic
3
4Module Program
5    Sub Main()
6        Dim numbers As New List(Of Integer) From {1, 2, 3, 5, 8}
7        Console.WriteLine(String.Join(", ", numbers))
8    End Sub
9End Module

This is functionally similar to:

vbnet
1Dim numbers As New List(Of Integer)
2numbers.Add(1)
3numbers.Add(2)
4numbers.Add(3)

The collection initializer is just shorter and easier to scan.

Initialize Lists of Objects Inline

The syntax becomes even more useful when combined with object initializers.

vbnet
1Imports System
2Imports System.Collections.Generic
3
4Public Class User
5    Public Property Id As Integer
6    Public Property Name As String
7End Class
8
9Module Program
10    Sub Main()
11        Dim users As New List(Of User) From {
12            New User With {.Id = 1, .Name = "Ada"},
13            New User With {.Id = 2, .Name = "Linus"},
14            New User With {.Id = 3, .Name = "Mina"}
15        }
16
17        For Each u In users
18            Console.WriteLine($"{u.Id}: {u.Name}")
19        Next
20    End Sub
21End Module

This is a clean way to create small in-memory datasets, test fixtures, or seed values.

Arrays and Lists Solve Different Problems

Sometimes developers ask for inline list initialization when an array would better express the actual requirement.

A fixed-size array can be initialized like this:

vbnet
Dim values() As Integer = {10, 20, 30}

A mutable list looks like this:

vbnet
Dim values As New List(Of Integer) From {10, 20, 30}
values.Add(40)

If you plan to grow or shrink the collection, use List(Of T). If the size is fixed and that fact matters, an array may be the more honest type.

Repeating the Same Value Many Times

If you need the same value many times, writing the literal over and over in the initializer quickly becomes ugly. In that case, generate the values programmatically.

vbnet
1Imports System
2Imports System.Collections.Generic
3Imports System.Linq
4
5Module Program
6    Sub Main()
7        Dim repeated As New List(Of String)(Enumerable.Repeat("N/A", 5))
8        Console.WriteLine(String.Join(" | ", repeated))
9    End Sub
10End Module

That expresses the real intent much better than manually duplicating the same literal several times.

Know What the Compiler Is Doing

Collection initializer syntax is convenient, but it is not magic syntax detached from runtime behavior. The collection is still constructed first, and then the initializer items are effectively added one by one.

That means:

  • constructor logic still runs
  • object initializer logic still runs
  • invalid values can still trigger runtime errors

The syntax is compact, but the semantics are still normal executable code.

Use Initializers Where They Improve Readability

Collection initializers work best when:

  • the initial data set is small
  • the values are understandable at a glance
  • the declaration still reads more like data than like logic

If the initializer becomes very large or complicated, a helper method or builder function may be clearer than one huge From block.

Common Pitfalls

  • Using a list initializer when the data is actually fixed-size and should be an array.
  • Writing huge initializer blocks that are harder to read than a small factory method.
  • Repeating the same value manually when Enumerable.Repeat or a loop would express intent better.
  • Forgetting that collection initializers still execute constructors and Add logic.
  • Choosing inline initialization for cleverness instead of readability.

Summary

  • In VB.NET, inline list initialization uses From.
  • It works well for both simple values and lists of initialized objects.
  • Use lists for mutable collections and arrays for fixed-size data.
  • Generate repeated values programmatically rather than duplicating literals by hand.
  • Prefer collection initializers when they make the code easier to read, not just shorter.

Course illustration
Course illustration

All Rights Reserved.