List(Of T).Count Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Gets the number of elements actually contained in the List(Of T).
Assembly: mscorlib (in mscorlib.dll)
Capacity is the number of elements that the List(Of T) can store before resizing is required. Count is the number of elements that are actually in the List(Of T).
Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.
Retrieving the value of this property is an O(1) operation.
The following code example shows the value of the Count property at various points in the life of a list. After the list has been created and populated and its elements displayed, the Capacity and Count properties are displayed. These properties are displayed again after the TrimExcess method has been called, and again after the contents of the list are cleared.
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