C# ArrayList Capacity
Description
ArrayList Capacity
gets or sets the number of elements
that the ArrayList can contain.
Syntax
ArrayList.Capacity
has the following syntax.
public virtual int Capacity { get; set; }
Example
The capacity is the number of elements an ArrayList
can store before
allocating more memory. The element count is the current
number of elements an ArrayList
has.
using System; /*from w w w.j a v a2s. c o m*/
using System.Collections;
class MainClass {
public static void Main() {
ArrayList al = new ArrayList();
Console.WriteLine("Adding 6 elements");
al.Add('C');
al.Add('A');
al.Add('E');
al.Add('B');
al.Add('D');
al.Add('F');
Console.WriteLine("Add enough elements to force ArrayList to grow. Adding 20 more elements");
for(int i=0; i < 20; i++)
al.Add((char)('a' + i));
Console.WriteLine("Current capacity: " +
al.Capacity);
Console.WriteLine("Number of elements after adding 20: " +
al.Count);
Console.Write("Contents: ");
foreach(char c in al)
Console.Write(c + " ");
Console.WriteLine("\n");
}
}
The code above generates the following result.