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)操作,其中 nCount的。This method is an O(n) operation, where n is Count.

若要将 SortedList<TKey,TValue> 重置为其初始状态,请在调用 TrimExcess 方法之前调用 Clear 方法。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.

适用于

另请参阅