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.

 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> 可能很大,因此, TrimExcess 如果清單的容量超過90%,方法不會執行任何動作。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 在呼叫方法之前先呼叫方法 TrimExcessTo 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.

容量也可以使用屬性來設定 CapacityThe capacity can also be set using the Capacity property.