Listに要素を追加するには、Addメソッドなどを使用します。 インスタンスを作成した後でも任意に要素を追加できる点がListと配列の大きく異なる点です。
空のListを作成する・Listに要素を追加する
Imports System
Imports System.Collections.Generic
Class Sample
Shared Sub Main()
' Integer型を格納する空のListを作成
Dim list As New List(Of Integer)()
' Listに要素を3つ追加する
list.Add(1)
list.Add(3)
list.Add(5)
Console.WriteLine("list(0) -> {0}", list(0))
Console.WriteLine("list(1) -> {0}", list(1))
Console.WriteLine("list(2) -> {0}", list(2))
End Sub
End Class
実行結果
list[0] -> 1 list[1] -> 3 list[2] -> 5
Listの作成方法のバリエーションについては後述の§.インスタンスの作成で解説しています。