Copy SortedList to an Array, starting at the specified index in the array in CSharp
Description
The following code shows how to copy SortedList to an Array, starting at the specified index in the array.
Example
using System;/*from www .ja v a 2 s . c o m*/
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();
}
}
The code above generates the following result.