C# 列表默认容量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1762817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default Capacity of List
提问by Jawahar BABU
What is the default capacity of a List?
List 的默认容量是多少?
回答by Elisha
The default capacity of List is 4 items (after you insert an initial item, otherwise it's of 0 size)
List 的默认容量为 4 项(插入初始项后,否则为 0 大小)
var list = new List<int>();
list.Add(1);
Assert.AreEqual(4, list.Capacity);
回答by Mark Byers
Why don't you just try it?
你为什么不试试呢?
Console.WriteLine("Default capacity of a List: " + new List<int>().Capacity);
This answer will work on all versions of .NET that have List. On my version, it happens to be 0.
此答案适用于所有具有 List 的 .NET 版本。在我的版本中,它恰好是 0。
回答by Jon Skeet
According to the sample on the MSDN parameterless constructor documentation, the initial capacity of a list created with:
根据MSDN 无参数构造函数文档上的示例,使用以下方法创建的列表的初始容量:
List<string> x = new List<string>();
is 0. As far as I can tell, this isn't documented as a guarantee, nor is the resize policy documented (i.e. it may currently double with a minimum of 4, but in .NET 5.0 it could triple with a minimum of 128.) You shouldn't rely on this behaviour, basically.
是 0。据我所知,这并没有记录为保证,也没有记录调整大小策略(即它目前可能会翻倍,至少为 4,但在 .NET 5.0 中,它可能会增加三倍,至少为 128 .) 基本上你不应该依赖这种行为。
回答by Thorarin
Actually, it starts with a Capacity of 0. When you add the first element, the current implementation allocates a capacity of 4. After that, the capacity keeps doubling if expansion is needed, to guarantee amortized O(1) operation.
实际上,它从容量为 0 开始。当您添加第一个元素时,当前实现分配的容量为 4。此后,如果需要扩展,容量将继续翻倍,以保证摊销 O(1) 操作。
Keep in mind that this is the current behavior. You shouldn't rely on it to be the case. This should demonstrate the current behavior:
请记住,这是当前的行为。你不应该依赖它。这应该展示当前的行为:
List<int> list = new List<int>();
int capacity = list.Capacity;
Console.WriteLine("Capacity: " + capacity);
for (int i = 0; i < 100000; i++)
{
list.Add(i);
if (list.Capacity > capacity)
{
capacity = list.Capacity;
Console.WriteLine("Capacity: " + capacity);
}
}
回答by tomkuj
This all lies in one line for ensuring the capacity is able to store another element:
这一切都在一行中,以确保容量能够存储另一个元素:
int num = this._items.Length == 0 ? 4 : this._items.Length * 2;
Got this from the mscorlib 4.0.0.0 deassebled - of course, as Jon said, this cannot be guaranteed not to change in the future (so far it still stays at 0, 4, 8, 16...).
从 mscorlib 4.0.0.0 deassebled 得到这个 - 当然,正如 Jon 所说,这不能保证在未来不会改变(到目前为止它仍然保持在 0、4、8、16 ......)。
Of course, you could set it yourself so this can be 3, 9, 27 etc.
当然,您可以自己设置,因此可以是 3、9、27 等。
回答by evhen14
Capacity should be used if you know roughly how many items you want to store in List (or in Stack, or Queue).
如果您大致知道要在列表(或堆栈或队列)中存储多少项,则应使用容量。
In this case you will avoid memory copying. The memory copying happens because under the hood Lists (Stacks and Queues) rely on array to store their items. That array size is you capacity, but it's not the same as the list size. As size of list needs to be bigger than the size of array, the List implementation will allocate a bigger array (factor of 2 maybe less) and will copy all items from old array to the new one plus newly added items.
在这种情况下,您将避免内存复制。内存复制的发生是因为在引擎盖下列表(堆栈和队列)依赖数组来存储它们的项目。该数组大小是您的容量,但它与列表大小不同。由于列表的大小需要大于数组的大小,List 实现将分配一个更大的数组(因子 2 可能更少)并将旧数组中的所有项目复制到新数组以及新添加的项目。
So, if you know that you may have from, say, 50 to 60 items in your list, create a list with capacity 60 and no memory deallocation will happen.
因此,如果您知道您的列表中可能有 50 到 60 个项目,请创建一个容量为 60 的列表,并且不会发生内存释放。
Note: And it looks like Garbage Collector will not have to clean up old arrays
注意:看起来垃圾收集器不必清理旧数组
回答by Fatih GüRDAL
The capacity default is 0, but if you create a blank list [List1] as below. If the list you created has elements in [List2] as follows, the number of the elements you add becomes N over 2. The default capacity varies.
容量默认为 0,但如果您创建一个空白列表 [ List1] 如下。如果您创建的列表在 [ List2] 中有如下元素,则您添加的元素数量变为 N 超过 2。默认容量不同。
List<int> List1 = new List<int>(); //count=0, capacity=0
List<int> List2 = new List<int>(){ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //count=11, capacity=16
When the elements of Arraytype were added, the developers expanded the array with the ReSizemethod. It worked very slowly. While developing the List type, the capacity was increased to a certain extent. This ratio has been developed as N above 2.
当添加Array类型的元素时,开发人员使用ReSize方法扩展了数组。它的工作非常缓慢。在开发List类型的同时,容量也得到了一定程度的提升。该比率已发展为 N 大于 2。
If more than the number of members are added to the list, the current capacity is doubled. Capacity will not decrease if you delete some values from the list. Capacity only increases, not decreases. Even the Clearmethod does not affect it.
如果添加到列表中的成员多于数量,则当前容量将增加一倍。如果您从列表中删除一些值,容量不会减少。容量只会增加,不会减少。即使是清除方法也不影响它。