I have been looking at .NET's List and ArrayList implementations with Reflector.
When looking at the Add(T item) I ran across this.EnsureCapacity(this._size + 1):
public void Add(T item)
{
if (this._size == this._items.Length)
{
this.EnsureCapacity(this._size + 1);
}
this._items[this._size++] = item;
this._version++;
}
So EnsureCapacity looks like this:
private void EnsureCapacity(int min)
{
if (this._items.Length < min)
{
int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
if (num < min)
{
num = min;
}
this.Capacity = num;
}
}
Why does the internal Capacity default to 4 and then increment in multiples of 2(ie: double)?