Listに要素を追加するには、Addメソッドを使います。 このメソッドを使うと、Listの末尾(一番最後)に要素が追加されます。
Addメソッドを使ってListの末尾に要素を追加する
Imports System
Imports System.Collections.Generic
Class Sample
Shared Sub Main()
' 要素が格納されていない空のListを作成
Dim list As New List(Of String)()
' 要素を追加する
list.Add("Alice")
list.Add("Bob")
Print(list)
' 要素を追加する
list.Add("Charlie")
Print(list)
End Sub
' Listの内容を列挙して表示する
Shared Sub Print(ByVal list As List(Of String))
For Each e As String In list
Console.Write("{0}, ", e)
Next
Console.WriteLine()
End Sub
End Class
実行結果
Alice, Bob, Alice, Bob, Charlie,
追加する位置を指定して要素を挿入したい場合はInsertメソッドを使うことができます。