C#: watching List<T> grow geometrically

List<T> keeps its items in an array. Count is the number of items and Capacity is the size of the underlying array. When Count would exceed Capacity, the list allocates a new array with twice the capacity and copies the items into it.

C#
using System;
using System.Collections.Generic;

class Program {
  static void Main() {
    var list = new List<int>();
    int last = list.Capacity;
    Console.WriteLine("capacity " + last + " after 0 items");
    for (int i = 1; i <= 33; i++) {
      list.Add(i);
      if (list.Capacity != last) {
        last = list.Capacity;
        Console.WriteLine("capacity " + last + " after " + i + " items");
      }
    }
  }
}
Read the full lesson