List <T>调整大小时,添加了多少额外容量?

SFu*_*n28 3 .net

当列表调整大小,因为没有更多的容量,添加了多少容量?只有1?或者它是否增加容量(使总容量加倍)?

SLa*_*aks 8

容量将翻倍.

这由以下来源控制:

// Ensures that the capacity of this list is at least the given minimum
// value. If the currect capacity of the list is less than min, the
// capacity is increased to twice the current capacity or to min, 
// whichever is larger.
private void EnsureCapacity(int min) { 
    if (_items.Length < min) { 
        int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
        if (newCapacity < min) newCapacity = min; 
        Capacity = newCapacity;
    }
}
Run Code Online (Sandbox Code Playgroud)

_defaultCapacityconst int等于4.