Stack<T>.TrimExcess 方法
定义
public:
void TrimExcess();
public void TrimExcess ();
member this.TrimExcess : unit -> unit
Public Sub TrimExcess ()
注解
如果没有新元素添加到集合中,则可以使用此方法最大程度地减少集合的内存开销。This method can be used to minimize a collection's memory overhead if no new elements will be added to the collection. 但重新分配和复制较大的成本 Stack<T> 可能很大,因此, TrimExcess 如果列表的容量超过90%,此方法不会执行任何操作。The cost of reallocating and copying a large Stack<T> can be considerable, however, so the TrimExcess method does nothing if the list is at more than 90 percent of capacity. 这样可以避免因相对较小的收益而产生较大的重新分配成本。This avoids incurring a large reallocation cost for a relatively small gain.
此方法是 O (n) 操作,其中 n 是 Count 。This method is an O(n) operation, where n is Count.
若要将重置 Stack<T> 为其初始状态,请 Clear 在调用方法之前调用方法 TrimExcess 。To reset a Stack<T> to its initial state, call the Clear method before calling TrimExcess method. 修整空会将的 Stack<T> 容量设置 Stack<T> 为默认容量。Trimming an empty Stack<T> sets the capacity of the Stack<T> to the default capacity.