Export (0) Print
Expand All

Array.CopyTo Method (Array, Int64)

Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 64-bit integer.

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

[ComVisibleAttribute(false)] 
public void CopyTo (
	Array array,
	long index
)
/** @attribute ComVisibleAttribute(false) */ 
public void CopyTo (
	Array array, 
	long index
)
ComVisibleAttribute(false) 
public function CopyTo (
	array : Array, 
	index : long
)

Parameters

array

The one-dimensional Array that is the destination of the elements copied from the current Array.

index

A 64-bit integer that represents the index in array at which copying begins.

Exception typeCondition

ArgumentNullException

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

ArgumentOutOfRangeException

index is outside the range of valid indexes for array.

ArgumentException

array is multidimensional.

-or-

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

-or-

The number of elements in the source Array is greater than the available space from index to the end of the destination array.

ArrayTypeMismatchException

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

RankException

The source Array is multidimensional.

This method supports the System.Collections.ICollection interface. If implementing System.Collections.ICollection is not explicitly required, use Copy to avoid an extra indirection.

If this method throws an exception while copying, the state of array is undefined.

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

The following code example shows how to copy an Array to another Array.

using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes two new Arrays.
      Array mySourceArray=Array.CreateInstance( typeof(String), 6 );
      mySourceArray.SetValue( "three", 0 );
      mySourceArray.SetValue( "napping", 1 );
      mySourceArray.SetValue( "cats", 2 );
      mySourceArray.SetValue( "in", 3 );
      mySourceArray.SetValue( "the", 4 );
      mySourceArray.SetValue( "barn", 5 );
      Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumps", 4 );
      myTargetArray.SetValue( "over", 5 );
      myTargetArray.SetValue( "the", 6 );
      myTargetArray.SetValue( "lazy", 7 );
      myTargetArray.SetValue( "dog", 8 );

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

      // Copies the source Array to the target Array, starting at index 6.
      mySourceArray.CopyTo( myTargetArray, 6 );

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


   public static void PrintValues( Array myArr, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

 The target Array contains the following (before and after copying):
 The quick brown fox jumps over the lazy dog      
 The quick brown fox jumps over three napping cats in the barn
*/

import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes two new Arrays.
        Array mySourceArray = Array.CreateInstance(String.class.ToType(), 6);
        mySourceArray.SetValue("three", 0);
        mySourceArray.SetValue("napping", 1);
        mySourceArray.SetValue("cats", 2);
        mySourceArray.SetValue("in", 3);
        mySourceArray.SetValue("the", 4);
        mySourceArray.SetValue("barn", 5);
        Array myTargetArray = Array.CreateInstance(String.class.ToType(), 15);
        myTargetArray.SetValue("The", 0);
        myTargetArray.SetValue("quick", 1);
        myTargetArray.SetValue("brown", 2);
        myTargetArray.SetValue("fox", 3);
        myTargetArray.SetValue("jumps", 4);
        myTargetArray.SetValue("over", 5);
        myTargetArray.SetValue("the", 6);
        myTargetArray.SetValue("lazy", 7);
        myTargetArray.SetValue("dog", 8);
        // Displays the values of the Array.
        Console.WriteLine("The target Array contains the following (before and "
            + " after copying):");
        PrintValues(myTargetArray, ' ');
        // Copies the source Array to the target Array, starting at index 6.
        mySourceArray.CopyTo(myTargetArray, 6);
        // Displays the values of the Array.
        PrintValues(myTargetArray, ' ');
    } //main

    public static void PrintValues(Array myArr, char mySeparator)
    {
        System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
        int i = 0;
        int cols = myArr.GetLength(myArr.get_Rank() - 1);
        while (myEnumerator.MoveNext()) {
            if (i < cols) {
                i++;
            }
            else {
                Console.WriteLine();
                i = 1;
            }
            Console.Write("{0}{1}", System.Convert.ToString(mySeparator), 
                myEnumerator.get_Current());
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArray

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

import System;
public class SamplesArray  {

   public static function Main()  {

      // Creates and initializes two new Arrays.
      var mySourceArray : System.Array = System.Array.CreateInstance( System.String, 6 );
      mySourceArray.SetValue( "three", 0 );
      mySourceArray.SetValue( "napping", 1 );
      mySourceArray.SetValue( "cats", 2 );
      mySourceArray.SetValue( "in", 3 );
      mySourceArray.SetValue( "the", 4 );
      mySourceArray.SetValue( "barn", 5 );
      var myTargetArray : System.Array = System.Array.CreateInstance( System.String, 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumped", 4 );
      myTargetArray.SetValue( "over", 5 );
      myTargetArray.SetValue( "the", 6 );
      myTargetArray.SetValue( "lazy", 7 );
      myTargetArray.SetValue( "dog", 8 );

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

      // Copies the source Array to the target Array, starting at index 6.
      mySourceArray.CopyTo( myTargetArray, 6 );

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


   public static function PrintValues( myArr : System.Array , mySeparator : char  )  {
      var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
      var i : int= 0;
      var cols : int = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      }
      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
*/

The following code example shows how to copy an Array to another Array with a nonzero lower bound. Note that the entire source Array is copied, including empty elements that overwrite existing elements in the target Array.

public class SamplesArray2{

   public static void Main()  {
      // Creates and initializes the source Array.
      Array myArrayZero=Array.CreateInstance( typeof(String), 3 );
      myArrayZero.SetValue( "zero", 0 );
      myArrayZero.SetValue( "one", 1 );

      // Displays the source Array.
      Console.WriteLine( "The array with lower bound=0 contains:" );
      PrintIndexAndValues( myArrayZero );

      // Creates and initializes the target Array.
      int[] myArrLen = { 4 };
      int[] myArrLow = { 2 };
      Array myArrayTwo=Array.CreateInstance( typeof(String), myArrLen, myArrLow );
      myArrayTwo.SetValue( "two", 2 );
      myArrayTwo.SetValue( "three", 3 );
      myArrayTwo.SetValue( "four", 4 );
      myArrayTwo.SetValue( "five", 5 );

      // Displays the target Array.
      Console.WriteLine( "The array with lower bound=2 contains:" );
      PrintIndexAndValues( myArrayTwo );

      // Copies from the array with lower bound=0 to the array with lower bound=2.
      myArrayZero.CopyTo( myArrayTwo, 3 );

      // Displays the modified target Array.
      Console.WriteLine( "\nAfter copying to the target array from index 3:" );
      PrintIndexAndValues( myArrayTwo );
   }


   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/* 
This code produces the following output.

The array with lower bound=0 contains:
    [0]:    zero
    [1]:    one
    [2]:    
The array with lower bound=2 contains:
    [2]:    two
    [3]:    three
    [4]:    four
    [5]:    five

After copying to the target array from index 3:
    [2]:    two
    [3]:    zero
    [4]:    one
    [5]:
*/

public class SamplesArray2
{
    public static void main(String[] args)
    {
        // Creates and initializes the source Array.
        Array myArrayZero = Array.CreateInstance(String.class.ToType(), 3);
        myArrayZero.SetValue("zero", 0);
        myArrayZero.SetValue("one", 1);
        // Displays the source Array.
        Console.WriteLine("The array with lower bound=0 contains:");
        PrintIndexAndValues(myArrayZero);
        // Creates and initializes the target Array.
        int myArrLen[] = { 4 };
        int myArrLow[] = { 2 };
        Array myArrayTwo = Array.CreateInstance(String.class.ToType(), myArrLen,
            myArrLow);
        myArrayTwo.SetValue("two", 2);
        myArrayTwo.SetValue("three", 3);
        myArrayTwo.SetValue("four", 4);
        myArrayTwo.SetValue("five", 5);
        // Displays the target Array.
        Console.WriteLine("The array with lower bound=2 contains:");
        PrintIndexAndValues(myArrayTwo);
        // Copies from the array with lower bound=0 to the array with lower bound=2.
        myArrayZero.CopyTo(myArrayTwo, 3);
        // Displays the modified target Array.
        Console.WriteLine("\nAfter copying to the target array from index 3:");
        PrintIndexAndValues(myArrayTwo);
    } //main

    public static void PrintIndexAndValues(Array myArray)
    {
        for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); 
            i++) {
            Console.WriteLine("\t[{0}]:\t{1}", (Int32)i, System.Convert.ToString(
                myArray.GetValue(i)));
        }
    } //PrintIndexAndValues
} //SamplesArray2 

 /* This code produces the following output.
 
 The array with lower bound=0 contains:
     [0]:    zero
     [1]:    one
     [2]:    
 The array with lower bound=2 contains:
     [2]:    two
     [3]:    three
     [4]:    four
     [5]:    five
 
 After copying to the target array from index 3:
     [2]:    two
     [3]:    zero
     [4]:    one
     [5]:
 */

public class SamplesArray2{

   public static function Main()  {
      // Creates and initializes the source Array.
      var myArrayZero : System.Array = System.Array.CreateInstance( System.String, 3 );
      myArrayZero.SetValue( "zero", 0 );
      myArrayZero.SetValue( "one", 1 );

      // Displays the source Array.
      Console.WriteLine( "The array with lower bound=0 contains:" );
      PrintIndexAndValues( myArrayZero );

      // Creates and initializes the target Array.
      var myArrLen : int[] = [4];
      var myArrLow : int[] = [2];
      var myArrayTwo : System.Array = System.Array.CreateInstance( System.String, myArrLen, myArrLow );
      myArrayTwo.SetValue( "two", 2 );
      myArrayTwo.SetValue( "three", 3 );
      myArrayTwo.SetValue( "four", 4 );
      myArrayTwo.SetValue( "five", 5 );

      // Displays the target Array.
      Console.WriteLine( "The array with lower bound=2 contains:" );
      PrintIndexAndValues( myArrayTwo );

      // Copies from the array with lower bound=0 to the array with lower bound=2.
      myArrayZero.CopyTo( myArrayTwo, 3 );

      // Displays the modified target Array.
      Console.WriteLine( "\nAfter copying to the target array from index 3:" );
      PrintIndexAndValues( myArrayTwo );
   }


   public static function PrintIndexAndValues( myArray : System.Array )  {
      for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/* 
This code produces the following output.

The array with lower bound=0 contains:
    [0]:    zero
    [1]:    one
    [2]:    
The array with lower bound=2 contains:
    [2]:    two
    [3]:    three
    [4]:    four
    [5]:    five

After copying to the target array from index 3:
    [2]:    two
    [3]:    zero
    [4]:    one
    [5]:
*/

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft