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.
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.
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.
List
by passing an array of values to the List
constructor. We print the first value.List
constructor call. We print the first value with Console.WriteLine
.Add()
method and append strings to the String
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 Modulecarrot carrot carrot
List
examplesHere we create 3 integer lists, with 3 different initialization approaches. The shortest syntax comes first.
List
constructor.List
to have, and pass that to the List
constructor.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 Module20;30;500; 20;30;500; 20;30;500;
In this benchmark we see how a capacity can speed up list initialization. We create many Lists in a tight loop.
List
by passing an array to the List
constructor. Two allocations will occur per call.List
constructor. Then we directly invoke the Add()
method with the values.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 Module83.48 ns Initialize with one statement 20.41 ns Initialize with capacity, Add() calls
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.