2010-11-11 3 views

Répondre

8

La capacité doublera.

Ceci est contrôlé par la source suivante:

// 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; 
    } 
} 

_defaultCapacity est un const int égal à 4.

+1

Argghh, je devais vérifier :( – leppie

2

On dirait qu'il double, basé sur le code suivant:

int initialCapacity = 100; 
List<string> list = new List<string>(initialCapacity); 

Console.WriteLine(list.Capacity); 

for(int i = 0; i < list.Capacity; i++){ 
    list.Add("string " + i);  
} 

list.Add("another string"); 

Console.WriteLine(list.Capacity); // shows 200, changes based on initialCapacity 
4

Ce sont les méthodes de EnsureCpacity que voit réflecteur il. La taille doublera :)

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; 
    } 
} 
Questions connexes