.net 当列表<;T>;调整大小后,会增加多少额外容量?
当列表因没有多余容量而调整大小时,会添加多少容量?就一个?还是增加了容量(使总容量翻倍)?容量将翻倍 这由以下来源控制:.net 当列表<;T>;调整大小后,会增加多少额外容量?,.net,.net,当列表因没有多余容量而调整大小时,会添加多少容量?就一个?还是增加了容量(使总容量翻倍)?容量将翻倍 这由以下来源控制: // 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 cap
// 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;
}
}
//确保此列表的容量至少为给定的最小值
//价值观。如果列表的当前容量小于最小值,则
//容量增加到当前容量的两倍或最小值,
//以较大者为准。
私人无效保证资本(整数最小值){
如果(_items.Length
\u defaultCapacity
是一个常数int
等于4
通常为双倍。根据以下代码,它看起来是双倍的:
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
int initialCapacity=100;
列表=新列表(初始容量);
控制台写入线(列表容量);
for(int i=0;i
这是reflector认为的“确保容量”方法。大小将加倍:)
private void-rescapacity(int-min)
{
如果(此项长度
感谢所有人的快速响应!我应该考虑使用反射器。我希望我能接受多个答案。
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;
}
}