Does the List in .NET work the same way as arraylist in Java?

.net, arraylist, java, list

Solution

You can easily test this by querying a List's `Capacity`:

    var a = new List<string>();

    Console.WriteLine(a.Capacity); // Writes 0

    a.Add("abc");

    Console.WriteLine(a.Capacity); // Writes 4

    a.Add("abc");
    a.Add("abc");
    a.Add("abc");
    a.Add("abc");

    Console.WriteLine(a.Capacity); // Writes 8

So it doesn't allocate any room at all upon instantiation, but upon first added item. From 8 it grows to 16, 32, etc...

Problem

When I learned Java, I was told that the arraylist works this way: It creates an array with room for 10 elements. When the 11th element is added, it is created a new list with room for 20 elements, and the 10 elements are copied into the new array. This will repeat as until there are no more elements to add, or to a maximum size. Is the List in .NET constructed the same way?

Original source