List(Of T).Add Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Adds an object to the end of the List(Of T).
Assembly: mscorlib (in mscorlib.dll)
Parameters
- item
- Type: T
The object to be added to the end of the List(Of T). The value can be Nothing for reference types.
Implements
ICollection(Of T).Add(T)List(Of T) accepts Nothing as a valid value for reference types and allows duplicate elements.
If Count already equals Capacity, the capacity of the List(Of T) is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.
If Count is less than Capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
The following code example demonstrates several properties and methods of the List(Of T) generic class, including the Add method. The default constructor is used to create a list of strings with a capacity of 0. The Capacity property is displayed, and then the Add method is used to add several items. The items are listed, and the Capacity property is displayed again, along with the Count property, to show that the capacity has been increased as needed.
Other properties and methods are used to search for, insert, and remove elements from the list, and finally to clear the list.
Imports System.Collections.Generic Public Class Example Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim dinosaurs As New List(Of String) outputBlock.Text &= String.Format(vbLf & "Capacity: {0}", dinosaurs.Capacity) & vbCrLf dinosaurs.Add("Tyrannosaurus") dinosaurs.Add("Amargasaurus") dinosaurs.Add("Mamenchisaurus") dinosaurs.Add("Deinonychus") dinosaurs.Add("Compsognathus") outputBlock.Text &= vbCrLf For Each dinosaur As String In dinosaurs outputBlock.Text &= dinosaur & vbCrLf Next outputBlock.Text &= String.Format(vbLf & "Capacity: {0}", dinosaurs.Capacity) & vbCrLf outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf outputBlock.Text &= vbLf & String.Format("Contains(""Deinonychus"": {0}", _ dinosaurs.Contains("Deinonychus")) & vbCrLf outputBlock.Text &= String.Format(vbLf & "Insert(2, ""Compsognathus"")") & vbCrLf dinosaurs.Insert(2, "Compsognathus") outputBlock.Text &= vbCrLf For Each dinosaur As String In dinosaurs outputBlock.Text &= dinosaur & vbCrLf Next outputBlock.Text &= String.Format(vbLf & "dinosaurs(3): {0}", dinosaurs(3)) & vbCrLf outputBlock.Text &= vbLf & "Remove(""Compsognathus"")" & vbCrLf dinosaurs.Remove("Compsognathus") outputBlock.Text &= vbCrLf For Each dinosaur As String In dinosaurs outputBlock.Text &= dinosaur & vbCrLf Next dinosaurs.TrimExcess() outputBlock.Text &= vbLf & "TrimExcess()" & vbCrLf outputBlock.Text &= String.Format("Capacity: {0}", dinosaurs.Capacity) & vbCrLf outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf dinosaurs.Clear() outputBlock.Text &= vbLf & "Clear()" & vbCrLf outputBlock.Text &= String.Format("Capacity: {0}", dinosaurs.Capacity) & vbCrLf outputBlock.Text &= String.Format("Count: {0}", dinosaurs.Count) & vbCrLf End Sub End Class ' This code example produces the following output: ' 'Capacity: 0 ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Deinonychus 'Compsognathus ' 'Capacity: 8 'Count: 5 ' 'Contains("Deinonychus"): True ' 'Insert(2, "Compsognathus") ' 'Tyrannosaurus 'Amargasaurus 'Compsognathus 'Mamenchisaurus 'Deinonychus 'Compsognathus ' 'dinosaurs(3): Mamenchisaurus ' 'Remove("Compsognathus") ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Deinonychus 'Compsognathus ' 'TrimExcess() 'Capacity: 5 'Count: 5 ' 'Clear() 'Capacity: 5 'Count: 0