C# List Capacity
Description
List
gets or sets the total number of
elements the internal data structure can hold without resizing.
Syntax
List.Capacity
has the following syntax.
public int Capacity { get; set; }
Example
List capacity tells us the number of elements a list can have without allocating more memory, while the count tells us the element count in a list.
using System;//from w w w. j av a2 s.co m
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> letters = new List<string>();
Console.WriteLine("\nCapacity: {0}", letters.Capacity);
letters.Add("A");
letters.Add("B");
letters.Add("C");
letters.Add("D");
letters.Add("E");
Console.WriteLine("Capacity: {0}", letters.Capacity);
Console.WriteLine("Count: {0}", letters.Count);
}
}
The code above generates the following result.