SortedList.TrimToSize Method
Sets the capacity to the actual number of elements in the SortedList.
[Visual Basic] Public Overridable Sub TrimToSize() [C#] public virtual void TrimToSize(); [C++] public: virtual void TrimToSize(); [JScript] public function TrimToSize();
Exceptions
| Exception Type | Condition |
|---|---|
| NotSupportedException | The SortedList is read-only.
-or- The SortedList has a fixed size. |
Remarks
This method can be used to minimize a list's memory overhead if no new elements will be added to the list.
To completely clear all elements in a list, call the Clear method before calling TrimToSize. Trimming an empty SortedList sets the capacity of the SortedList to the default capacity, not zero.
Example
[Visual Basic, C#, C++] The following example shows how to trim the unused portions of the SortedList and how to clear the values of the SortedList.
[Visual Basic] Imports System Imports System.Collections Imports Microsoft.VisualBasic Public Class SamplesSortedList Public Shared Sub Main() ' Creates and initializes a new SortedList. Dim mySL As New SortedList() mySL.Add("one", "The") mySL.Add("two", "quick") mySL.Add("three", "brown") mySL.Add("four", "fox") mySL.Add("five", "jumped") ' Displays the count, capacity and values of the SortedList. Console.WriteLine("Initially,") Console.WriteLine(" Count : {0}", mySL.Count) Console.WriteLine(" Capacity : {0}", mySL.Capacity) Console.WriteLine(" Values:") PrintKeysAndValues(mySL) ' Trims the SortedList. mySL.TrimToSize() ' Displays the count, capacity and values of the SortedList. Console.WriteLine("After TrimToSize,") Console.WriteLine(" Count : {0}", mySL.Count) Console.WriteLine(" Capacity : {0}", mySL.Capacity) Console.WriteLine(" Values:") PrintKeysAndValues(mySL) ' Clears the SortedList. mySL.Clear() ' Displays the count, capacity and values of the SortedList. Console.WriteLine("After Clear,") Console.WriteLine(" Count : {0}", mySL.Count) Console.WriteLine(" Capacity : {0}", mySL.Capacity) Console.WriteLine(" Values:") PrintKeysAndValues(mySL) ' Trims the SortedList again. mySL.TrimToSize() ' Displays the count, capacity and values of the SortedList. Console.WriteLine("After the second TrimToSize,") Console.WriteLine(" Count : {0}", mySL.Count) Console.WriteLine(" Capacity : {0}", mySL.Capacity) Console.WriteLine(" Values:") PrintKeysAndValues(mySL) End Sub Public Shared Sub PrintKeysAndValues(myList As SortedList) Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _ "-VALUE-") Dim i As Integer For i = 0 To myList.Count - 1 Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _ "{1}", myList.GetKey(i), myList.GetByIndex(i)) Next i Console.WriteLine() End Sub End Class ' This code produces the following output. ' ' Initially, ' Count : 5 ' Capacity : 16 ' Values: ' -KEY- -VALUE- ' five: jumped ' four: fox ' one: The ' three: brown ' two: quick ' ' After TrimToSize, ' Count : 5 ' Capacity : 5 ' Values: ' -KEY- -VALUE- ' five: jumped ' four: fox ' one: The ' three: brown ' two: quick ' ' After Clear, ' Count : 0 ' Capacity : 16 ' Values: ' -KEY- -VALUE- ' ' ' After the second TrimToSize, ' Count : 0 ' Capacity : 16 ' Values: ' -KEY- -VALUE- [C#] using System; using System.Collections; public class SamplesSortedList { public static void Main() { // Creates and initializes a new SortedList. SortedList mySL = new SortedList(); mySL.Add( "one", "The" ); mySL.Add( "two", "quick" ); mySL.Add( "three", "brown" ); mySL.Add( "four", "fox" ); mySL.Add( "five", "jumped" ); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "Initially," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList. mySL.TrimToSize(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After TrimToSize," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Clears the SortedList. mySL.Clear(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After Clear," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList again. mySL.TrimToSize(); // Displays the count, capacity and values of the SortedList. Console.WriteLine( "After the second TrimToSize," ); Console.WriteLine( " Count : {0}", mySL.Count ); Console.WriteLine( " Capacity : {0}", mySL.Capacity ); Console.WriteLine( " Values:" ); PrintKeysAndValues( mySL ); } public static void PrintKeysAndValues( SortedList myList ) { Console.WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) ); } Console.WriteLine(); } } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After TrimToSize, Count : 5 Capacity : 5 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After Clear, Count : 0 Capacity : 16 Values: -KEY- -VALUE- After the second TrimToSize, Count : 0 Capacity : 16 Values: -KEY- -VALUE- */ [C++] #using <mscorlib.dll> #using <system.dll> using namespace System; using namespace System::Collections; void PrintKeysAndValues( SortedList *myList ) { Console::WriteLine( "\t-KEY-\t-VALUE-" ); for ( int i = 0; i < myList->Count; i++ ) { Console::WriteLine( "\t{0}:\t{1}", myList->GetKey(i), myList->GetByIndex(i) ); } Console::WriteLine(); } int main() { // Creates and initializes a new SortedList. SortedList __gc *mySL = new SortedList(); mySL->Add( S"one", S"The" ); mySL->Add( S"two", S"quick" ); mySL->Add( S"three", S"brown" ); mySL->Add( S"four", S"fox" ); mySL->Add( S"five", S"jumped" ); // Displays the count, capacity and values of the SortedList. Console::WriteLine( "Initially," ); Console::WriteLine( " Count : {0}", __box(mySL->Count) ); Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) ); Console::WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList. mySL->TrimToSize(); // Displays the count, capacity and values of the SortedList. Console::WriteLine( "After TrimToSize," ); Console::WriteLine( " Count : {0}", __box(mySL->Count) ); Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) ); Console::WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Clears the SortedList. mySL->Clear(); // Displays the count, capacity and values of the SortedList. Console::WriteLine( "After Clear," ); Console::WriteLine( " Count : {0}", __box(mySL->Count) ); Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) ); Console::WriteLine( " Values:" ); PrintKeysAndValues( mySL ); // Trims the SortedList again. mySL->TrimToSize(); // Displays the count, capacity and values of the SortedList. Console::WriteLine( "After the second TrimToSize," ); Console::WriteLine( " Count : {0}", __box(mySL->Count) ); Console::WriteLine( " Capacity : {0}", __box(mySL->Capacity) ); Console::WriteLine( " Values:" ); PrintKeysAndValues( mySL ); } /* This code produces the following output. Initially, Count : 5 Capacity : 16 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After TrimToSize, Count : 5 Capacity : 5 Values: -KEY- -VALUE- five: jumped four: fox one: The three: brown two: quick After Clear, Count : 0 Capacity : 16 Values: -KEY- -VALUE- After the second TrimToSize, Count : 0 Capacity : 16 Values: -KEY- -VALUE- */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
SortedList Class | SortedList Members | System.Collections Namespace | Clear | Capacity | Count