Clone a SortedList in CSharp

Description

The following code shows how to clone a SortedList.

Example


using System;/*from w  w  w .j av  a 2  s.  c  o m*/
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {
      SortedList mySL = new SortedList();
      mySL.Add( "one", "1" );
      mySL.Add( "two", "2" );
      mySL.Add( "three", "3" );


      for (int i = 0; i < mySL.Count; i++)
      {
            Console.WriteLine("{0,-6}: {1}",mySL.GetKey(i), mySL.GetByIndex(i));
      }  

      mySL = (SortedList)mySL.Clone();

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

      for (int i = 0; i < mySL.Count; i++)
      {
            Console.WriteLine("{0,-6}: {1}",mySL.GetKey(i), mySL.GetByIndex(i));
      }  
   }
}

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