SortedList<TKey,TValue>.TrimExcess 方法

定义

如果元素数小于当前容量的 90%,将容量设置为 SortedList<TKey,TValue> 中的实际元素数。Sets the capacity to the actual number of elements in the SortedList<TKey,TValue>, if that number is less than 90 percent of current capacity.

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. 但重新分配和复制较大SortedList<TKey,TValue>的成本可能很大,因此,如果列表的容量超过 90%,此TrimExcess方法不会执行任何操作。The cost of reallocating and copying a large SortedList<TKey,TValue> 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)运算,其中nCountThis method is an O(n) operation, where n is Count.

若要将SortedList<TKey,TValue>重置为其初始状态, Clear请在调用TrimExcess方法之前调用方法。To reset a SortedList<TKey,TValue> to its initial state, call the Clear method before calling TrimExcess method. 修整空SortedList<TKey,TValue>会将的容量SortedList<TKey,TValue>设置为默认容量。Trimming an empty SortedList<TKey,TValue> sets the capacity of the SortedList<TKey,TValue> to the default capacity.

还可以使用Capacity属性设置容量。The capacity can also be set using the Capacity property.

适用于

另请参阅