List.Capacity Property In VB.NET
In this article I describe about List.Capacity Property which gets and sets the number of items a list can hold without resizing.
Capacity
The Capacity property gets and sets the number of items a list can hold without resizing. Capacity is always greater than or equal to the Count value.
The following code snippet creates a List of authors and displays the original capacity. After that, code removes the excess capacity by calling TrimExcess method. After that, capacity is set to 20.
Imports System.Text
Imports System.IO
Imports System.Collections.Generic
Module Module1
Sub Main()
' Create a list of strings
Dim AuthorList As New List(Of String)()
AuthorList.Add("Mahesh Chand")
AuthorList.Add("Praveen Kumar")
AuthorList.Add("Raj Kumar")
AuthorList.Add("Nipun Tomar")
AuthorList.Add("Dinesh Beniwal")
' Original Capacity
Console.WriteLine("Original Capacity: {0}", AuthorList.Capacity)
' Trim excess
AuthorList.TrimExcess()
Console.WriteLine("Trimmed Capacity: {0}", AuthorList.Capacity)
' Update Capacity
AuthorList.Capacity = 20
Console.WriteLine(AuthorList.Capacity)
Console.WriteLine("Updated Capacity: {0}", AuthorList.Capacity)
Console.ReadLine()
End Sub
End Module
Output: