Set the capacity to the actual number of elements in a SortedList in CSharp

Description

The following code shows how to set the capacity to the actual number of elements in a SortedList.

Example


/*from   w  ww  .j  a v a  2 s  .c om*/
using System;
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 );

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary