C# SortedList CopyTo
Description
SortedList CopyTo
copies SortedList elements to a one-dimensional
Array object, starting at the specified index in the array.
Syntax
SortedList.CopyTo
has the following syntax.
public virtual void CopyTo(
Array array,
int arrayIndex
)
Parameters
SortedList.CopyTo
has the following parameters.
array
- The one-dimensional Array object that is the destination of the DictionaryEntry objects copied from SortedList. The Array must have zero-based indexing.arrayIndex
- The zero-based index in array at which copying begins.
Returns
SortedList.CopyTo
method returns
Example
SortedList.CopyTo
copies SortedList
elements to a one-dimensional Array object.
using System;// w w w. j av a 2 s. com
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.