Mega Code Archive

 
Categories / C# / Collections Data Structure
 

SortedList CopyTo copies SortedList elements to a one-dimensional Array object

using System;  using System.Collections;  public class SamplesSortedList  {     public static void Main()  {        SortedList mySourceList = new SortedList();        mySourceList.Add( 2, "A" );        mySourceList.Add( 3, "B" );        mySourceList.Add( 1, "C" );        String[] tempArray = new String[] { "This", "is", "a", "test" };        DictionaryEntry[] myTargetArray = new DictionaryEntry[15];        int i = 0;        foreach ( String s in tempArray )  {           myTargetArray[i].Key = i;           myTargetArray[i].Value = s;           i++;        }        mySourceList.CopyTo( myTargetArray, 6 );        PrintValues( myTargetArray, ' ' );     }     public static void PrintValues( DictionaryEntry[] myArr, char mySeparator )  {        for ( int i = 0; i < myArr.Length; i++ )           Console.Write( "{0}{1}", mySeparator, myArr[i].Value );        Console.WriteLine();     }  }