Home
VB.NET
List Initialize
This page was last reviewed on Feb 1, 2024.
Dot Net Perls
Initialize List. A VB.NET List starts out empty. But we can initialize it in a single line with an initializer. This makes programs easier to read and shorter.
Shows a list
And with the List constructor, we can use a capacity to improve performance. For optimal VB.NET programs, using a capacity and the Add() method is a good choice.
List
First example. Sometimes we want a String List. We can use several syntax forms for String Lists—these same syntax forms can be used for other Lists.
Version 1 We create a List by passing an array of values to the List constructor. We print the first value.
Version 2 We use a more verbose syntax for the List constructor call. We print the first value with Console.WriteLine.
Version 3 We directly call the Add() method and append strings to the String list.
List Add
Shows a list
Module Module1 Sub Main() ' Version 1. Dim list As New List(Of String)({"carrot", "fox", "explorer"}) Console.WriteLine(list(0)) ' Version 2. Dim list2 As New List(Of String)(New String() {"carrot", "fox", "explorer"}) Console.WriteLine(list2(0)) ' Version 3. Dim list3 As New List(Of String)() list3.Add("carrot") list3.Add("fox") list3.Add("explorer") Console.WriteLine(list3(0)) End Sub End Module
carrot carrot carrot
Integer List examples. Here we create 3 integer lists, with 3 different initialization approaches. The shortest syntax comes first.
Part 1 Here we use an expression. We can omit the "New Integer" part of the argument to the List constructor.
Part 2 We can create an array containing the elements we want the List to have, and pass that to the List constructor.
Part 3 We can add elements to a list with imperative statements (the Add method).
Module Module1 Sub Main() ' Part 1: initialize with curly brackets. Dim list As New List(Of Integer)({20, 30, 500}) For Each element In list Console.Write(element) Console.Write(";") Next Console.WriteLine() ' Part 2: initialize with a temporary array. Dim list2 As New List(Of Integer)(New Integer() {20, 30, 500}) For Each element In list2 Console.Write(element) Console.Write(";") Next Console.WriteLine() ' Part 3: initialize with Add calls. Dim list3 As New List(Of Integer)() list3.Add(20) list3.Add(30) list3.Add(500) For Each element In list3 Console.Write(element) Console.Write(";") Next Console.WriteLine() End Sub End Module
20;30;500; 20;30;500; 20;30;500;
Benchmark, initialization. In this benchmark we see how a capacity can speed up list initialization. We create many Lists in a tight loop.
Benchmark
Version 1 This version of the code creates a List by passing an array to the List constructor. Two allocations will occur per call.
Version 2 Here we set a capacity of 5 in the List constructor. Then we directly invoke the Add() method with the values.
Result Version 2 is 4 times faster than version 1. A capacity will speed up List programs.
Module Module1 Sub Main() Dim m As Integer = 10000000 A() B() Dim s1 As Stopwatch = Stopwatch.StartNew ' Version 1: initialize list with an array argument. For i As Integer = 0 To m - 1 A() Next s1.Stop() Dim s2 As Stopwatch = Stopwatch.StartNew ' Version 2: initialize list with Add() calls. For i As Integer = 0 To m - 1 B() Next s2.Stop() Dim u As Integer = 1000000 Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) End Sub Sub A() ' Add with initialization statement. Dim a As List(Of Integer) = New List(Of Integer)({400, 500, 600, 700, 800}) If Not a(0) = 400 Then Console.WriteLine("X") End If End Sub Sub B() ' Add with Add() calls, specify capacity. Dim a As List(Of Integer) = New List(Of Integer)(5) a.Add(400) a.Add(500) a.Add(600) a.Add(700) a.Add(800) If Not a(0) = 400 Then Console.WriteLine("X") End If End Sub End Module
83.48 ns Initialize with one statement 20.41 ns Initialize with capacity, Add() calls
A summary. In VB.NET programs we often use Lists. These contain Integers, Strings and many other types of elements. With a capacity, we can initialize lists in the fastest way.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 1, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.