C# SortedList TrimToSize

Description

SortedList TrimToSize sets the capacity to the actual number of elements in a SortedList object.

Syntax

SortedList.TrimToSize has the following syntax.


public virtual void TrimToSize()

Returns

SortedList.TrimToSize method returns

Example

The following code example shows how to trim the unused portions of a SortedList object and how to clear its values.


using System;//from   ww  w. jav  a2s  . c o  m
using System.Collections;
public class SamplesSortedList  {
   public static void Main()  {
      SortedList mySL = new SortedList();
      mySL.Add( "one", "The" );
      mySL.Add( "two", "quick" );
      mySL.Add( "three", "brown" );
      mySL.Add( "four", "fox" );
      mySL.Add( "five", "jumped" );

      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );

      mySL.TrimToSize();

      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );

   }
   public static void PrintKeysAndValues( SortedList myList )  {
      for ( int i = 0; i < myList.Count; i++ )  {
         Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack