Export (0) Print
Expand All

SortedList.CopyTo Method

Copies SortedList elements to a one-dimensional Array object, starting at the specified index in the array.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual void CopyTo (
	Array array,
	int arrayIndex
)
public void CopyTo (
	Array array, 
	int arrayIndex
)
public function CopyTo (
	array : Array, 
	arrayIndex : int
)
Not applicable.

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.

Exception typeCondition

ArgumentNullException

array is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

arrayIndex is less than zero.

ArgumentException

array is multidimensional.

-or-

arrayIndex is equal to or greater than the length of array.

-or-

The number of elements in the source SortedList object is greater than the available space from arrayIndex to the end of the destination array.

InvalidCastException

The type of the source SortedList cannot be cast automatically to the type of the destination array.

The key/value pairs are copied to the Array object in the same order in which the enumerator iterates through the SortedList object.

To copy only the keys in the SortedList, use SortedList.Keys.CopyTo.

To copy only the values in the SortedList, use SortedList.Values.CopyTo.

This method is an O(n) operation, where n is Count.

The following code example shows how to copy the values in a SortedList object into a one-dimensional Array object.

 using System;
 using System.Collections;
 public class SamplesSortedList  {
 
    public static void Main()  {
 
       // Creates and initializes the source SortedList.
       SortedList mySourceList = new SortedList();
       mySourceList.Add( 2, "cats" );
       mySourceList.Add( 3, "in" );
       mySourceList.Add( 1, "napping" );
       mySourceList.Add( 4, "the" );
       mySourceList.Add( 0, "three" );
       mySourceList.Add( 5, "barn" );
 
       // Creates and initializes the one-dimensional target Array.
       String[] tempArray = new String[] { "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" };
       DictionaryEntry[] myTargetArray = new DictionaryEntry[15];
       int i = 0;
       foreach ( String s in tempArray )  {
          myTargetArray[i].Key = i;
          myTargetArray[i].Value = s;
          i++;
       }

       // Displays the values of the target Array.
       Console.WriteLine( "The target Array contains the following (before and after copying):" );
       PrintValues( myTargetArray, ' ' );
 
       // Copies the entire source SortedList to the target SortedList, starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       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();
    }

 }


/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn

*/ 

import System.*;
import System.Collections.*;

public class SamplesSortedList
{

    public static void main(String[] args)
    {
        // Creates and initializes the source SortedList.
        SortedList mySourceList = new SortedList();

        mySourceList.Add((Int32)2, "cats");
        mySourceList.Add((Int32)3, "in");
        mySourceList.Add((Int32)1, "napping");
        mySourceList.Add((Int32)4, "the");
        mySourceList.Add((Int32)0, "three");
        mySourceList.Add((Int32)5, "barn");

        // Creates and initializes the one-dimensional target Array.
        String tempArray[] = new String[] {"The", "quick", "brown", "fox", 
            "jumped", "over", "the", "lazy", "dog"};
        DictionaryEntry myTargetArray[] = new DictionaryEntry[15];

        for (int iCtr = 0; iCtr < tempArray.length; iCtr++) {
            String s = tempArray[iCtr];
            myTargetArray[iCtr].set_Key((Int32)iCtr);
            myTargetArray[iCtr].set_Value(s);
        }

        // Displays the values of the target Array.
        Console.WriteLine("The target Array contains the following " 
            + "(before and after copying):");
        PrintValues(myTargetArray, ' ');

        // Copies the entire source SortedList to the target SortedList, 
        // starting at index 6.
        mySourceList.CopyTo(myTargetArray, 6);

        // Displays the values of the target Array.
        PrintValues(myTargetArray, ' ');
    } //main

    public static void PrintValues(DictionaryEntry[] myArr, char mySeparator)
    {
        for (int i = 0; i < myArr.length; i++) {
            Console.Write("{0}{1}", 
                System.Convert.ToString(mySeparator), 
                System.Convert.ToString(myArr[i].get_Value()));
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesSortedList

/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over three napping cats in the barn

 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft