HomeSearch

VB.NET Initialize List

Initialize Lists of Strings and Integers, using capacity to make List initialization faster.

Initialize List. A list starts out empty. But we can initialize it in a single line with an initializer. This makes programs easier to read and shorter.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.

Integer List examples. Here we create 3 integer lists, with 3 different initialization approaches. The shortest syntax comes first.

Part A: Here we use an expression. We can omit the "New Integer" part of the argument to the List constructor.

Part B: We can create an array containing the elements we want the List to have, and pass that to the List constructor.

Part C: We can add elements to a list with imperative statements (the Add method).

VB.NET program that uses Integer List initialization Module Module1 Sub Main() ' Part A: 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 B: 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 C: 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 Output 20;30;500; 20;30;500; 20;30;500;

String Lists. Sometimes we are creating lists with no Integers. We want a String List. This uses the same syntax as an Integer List, but with the String type name.

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.

VB.NET program that uses String List initialization Module Module1 Sub Main() ' Version 1. Dim list As New List(Of String)({"bird", "fish", "bear"}) Console.WriteLine(list(0)) ' Version 2. Dim list2 As New List(Of String)(New String() {"bird", "fish", "bear"}) Console.WriteLine(list2(0)) ' Version 3. Dim list3 As New List(Of String)() list3.Add("bird") list3.Add("fish") list3.Add("bear") Console.WriteLine(list3(0)) End Sub End Module Output bird bird bird

Benchmark, initialization. In this benchmark we see how a capacity can speed up list initialization. We create many Lists in a tight loop.Benchmarks

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.

VB.NET program that benchmarks List initialization 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 Output 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.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.