我知道Stack<T>
的基础存储空间为T[]
,但它的默认大小是多少?
答案 0 :(得分:2)
stack.cs文件在System.Collections.Stack中显示此信息:
private const int _defaultCapacity = 10;
在System.Collections.Generic.Stack中,默认值为4
private const int _defaultCapacity = 4;
参考:https://referencesource.microsoft.com/#mscorlib/system/collections/stack.cs https://referencesource.microsoft.com/#System/compmod/system/collections/generic/stack.cs,c5371bef044c6ab6
答案 1 :(得分:1)
当您询问默认尺寸(不是容量)时,答案是零。
反编译我的版本我看到了:
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1"/> class that is empty and has the default initial capacity.
/// </summary>
[__DynamicallyInvokable]
public Stack()
{
this._array = Stack<T>._emptyArray;
this._size = 0;
this._version = 0;
}
private static T[] _emptyArray = new T[0];
但我不确定这是否有记录,所以你的里程数会有所不同。