Array.CopyTo Array.CopyTo Array.CopyTo Method

정의

현재 1차원 배열의 모든 요소를 지정된 1차원 배열에 복사합니다.Copies all the elements of the current one-dimensional array to the specified one-dimensional array.

오버로드

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

현재 1차원 배열의 모든 요소를 지정된 대상 배열 인덱스부터 시작하여 지정된 1차원 배열에 복사합니다.Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. 인덱스가 32비트 정수로 지정되어 있습니다.The index is specified as a 32-bit integer.

CopyTo(Array, Int64) CopyTo(Array, Int64) CopyTo(Array, Int64)

현재 1차원 배열의 모든 요소를 지정된 대상 배열 인덱스부터 시작하여 지정된 1차원 배열에 복사합니다.Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. 인덱스가 64비트 정수로 지정되어 있습니다.The index is specified as a 64-bit integer.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

현재 1차원 배열의 모든 요소를 지정된 대상 배열 인덱스부터 시작하여 지정된 1차원 배열에 복사합니다.Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. 인덱스가 32비트 정수로 지정되어 있습니다.The index is specified as a 32-bit integer.

public:
 virtual void CopyTo(Array ^ array, int index);
public virtual void CopyTo (Array array, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit

매개 변수

array
Array Array Array

현재 배열에서 복사한 요소의 대상인 1차원 배열입니다.The one-dimensional array that is the destination of the elements copied from the current array.

index
Int32 Int32 Int32

복사가 시작되는 array의 인덱스를 나타내는 32비트 정수입니다.A 32-bit integer that represents the index in array at which copying begins.

구현

예외

arraynull인 경우array is null.

indexarray의 하한값보다 작습니다.index is less than the lower bound of array.

array가 다차원 배열인 경우array is multidimensional.

또는-or- 소스 배열의 요소 개수가 index부터 대상 array 끝까지의 사용 가능한 요소 개수보다 큽니다.The number of elements in the source array is greater than the available number of elements from index to the end of the destination array.

소스 Array의 형식을 대상 array의 형식으로 자동 캐스팅할 수 없습니다.The type of the source Array cannot be cast automatically to the type of the destination array.

소스 배열이 다차원입니다.The source array is multidimensional.

Array 소스에서 하나 이상의 요소를 array의 대상 형식으로 캐스팅할 수 없습니다.At least one element in the source Array cannot be cast to the type of destination array.

예제

다음 코드 예제에서는 복사 하는 방법을 보여 줍니다.는 ArrayArray합니다.The following code example shows how to copy an Array to another Array.

using namespace System;
void main1();
void main2();
void main()
{
   main1();
   Console::WriteLine();
   main2();
}

void PrintValues( Array^ myArr );
void main1()
{
   // Creates and initializes two new Array instances.
   Array^ mySourceArray = Array::CreateInstance( String::typeid, 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::typeid, 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 instance 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 );
}

void PrintValues( Array^ myArr )
{
   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}", myEnumerator->Current );
   }

   Console::WriteLine();
}


/*
 This code produces the following output.
 
  The target Array instance 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
 */
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
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes two new Arrays.
        Dim mySourceArray As Array = Array.CreateInstance(GetType(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)
        Dim myTargetArray As Array = Array.CreateInstance(GetType(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, " "c)
        
        ' Copies the source Array to the target Array, starting at index 6.
        mySourceArray.CopyTo(myTargetArray, 6)
        
        ' Displays the values of the Array.
        PrintValues(myTargetArray, " "c)
    End Sub    
    
    Public Shared Sub PrintValues(myArr As Array, mySeparator As Char)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' 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

다음 코드 예제에서는 복사 하는 방법을 보여 줍니다.는 ArrayArray 0이 아닌을 사용 하 여 합니다.The following code example shows how to copy an Array to another Array with a nonzero lower bound. 전체 소스 Array 대상에서 기존 요소를 덮어쓰지는 빈 요소를 포함 하 여 복사 됩니다 Array합니다.Note that the entire source Array is copied, including empty elements that overwrite existing elements in the target Array.

void PrintIndexAndValues( Array^ myArray );
void main2()
{
   // Creates and initializes the source Array.
   Array^ myArrayZero = Array::CreateInstance( String::typeid, 3 );
   myArrayZero->SetValue( "zero", 0 );
   myArrayZero->SetValue( "one", 1 );

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

   // Creates and initializes the target Array.
   array<int>^myArrLen = {4};
   array<int>^myArrLow = {2};
   Array^ myArrayTwo = Array::CreateInstance( String::typeid, 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 lowbound=2 contains:" );
   PrintIndexAndValues( myArrayTwo );

   // Copy from the array with lowbound=0 to the array with lowbound=2.
   myArrayZero->CopyTo( myArrayTwo, 3 );

   // Displays the modified target Array.
   Console::WriteLine(  "\nAfter copying at relative index 1:" );
   PrintIndexAndValues( myArrayTwo );
}

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 lowbound=0 contains:
     [0]:    zero
     [1]:    one
     [2]:    
 The array with lowbound=2 contains:
     [2]:    two
     [3]:    three
     [4]:    four
     [5]:    five
 
 After copying at relative index 1:
     [2]:    two
     [3]:    zero
     [4]:    one
     [5]:
 */
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 Shared Sub Main()
        ' Creates and initializes the source Array.
        Dim myArrayZero As Array = Array.CreateInstance(GetType(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.
        Dim myArrLen As Integer() = {4}
        Dim myArrLow As Integer() = {2}
        Dim myArrayTwo As Array = Array.CreateInstance(GetType(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(ControlChars.Cr + "After copying to the target array from " _
           + "index 3:")
        PrintIndexAndValues(myArrayTwo)
    End Sub
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' 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]: 

설명

이 메서드는 현재 인스턴스의 배열 요소를 모두 복사 합니다 array 대상 배열 인덱스부터 index합니다.This method copies all the elements of the current array instance to the array destination array, starting at index index. array 대상 배열 해야 이미 있는 된 차원이 및 요소 복사 된 요소에 맞게 충분 한 수 있어야 합니다.The array destination array must already have been dimensioned and must have a sufficient number of elements to accommodate the copied elements. 그렇지 않으면 메서드에서 예외가 throw됩니다.Otherwise, the method throws an exception.

이 메서드는 System.Collections.ICollection 인터페이스입니다.This method supports the System.Collections.ICollection interface. 구현 하는 경우 System.Collections.ICollection 가 사용 하 여 명시적으로 필요한 Copy 추가 간접 참조를 방지 하려면.If implementing System.Collections.ICollection is not explicitly required, use Copy to avoid an extra indirection.

이 메서드는 상태를 복사 하는 동안 예외를 throw 하는 경우 array 정의 되지 않습니다.If this method throws an exception while copying, the state of array is undefined.

이 메서드는 O (n) 작업, 여기서 nLength합니다. 단순 복사만 수행합니다.This method is an O(n) operation, where n is Length.It performs a shallow copy only.

추가 정보

CopyTo(Array, Int64) CopyTo(Array, Int64) CopyTo(Array, Int64)

현재 1차원 배열의 모든 요소를 지정된 대상 배열 인덱스부터 시작하여 지정된 1차원 배열에 복사합니다.Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. 인덱스가 64비트 정수로 지정되어 있습니다.The index is specified as a 64-bit integer.

public:
 virtual void CopyTo(Array ^ array, long index);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void CopyTo (Array array, long index);
abstract member CopyTo : Array * int64 -> unit
override this.CopyTo : Array * int64 -> unit

매개 변수

array
Array Array Array

현재 배열에서 복사한 요소의 대상인 1차원 배열입니다.The one-dimensional array that is the destination of the elements copied from the current array.

index
Int64 Int64 Int64

복사가 시작되는 array의 인덱스를 나타내는 64비트 정수입니다.A 64-bit integer that represents the index in array at which copying begins.

예외

arraynull인 경우array is null.

indexarray의 유효한 인덱스 범위를 벗어납니다.index is outside the range of valid indexes for array.

array가 다차원 배열인 경우array is multidimensional.

또는-or- 소스 배열의 요소 개수가 index부터 대상 array 끝까지의 사용 가능한 요소 개수보다 큽니다.The number of elements in the source array is greater than the available number of elements from index to the end of the destination array.

소스 Array의 형식을 대상 array의 형식으로 자동 캐스팅할 수 없습니다.The type of the source Array cannot be cast automatically to the type of the destination array.

소스 Array가 다차원입니다.The source Array is multidimensional.

Array 소스에서 하나 이상의 요소를 array의 대상 형식으로 캐스팅할 수 없습니다.At least one element in the source Array cannot be cast to the type of destination array.

예제

다음 코드 예제에서는 복사 하는 방법을 보여 줍니다.는 ArrayArray합니다.The following code example shows how to copy an Array to another Array.

using namespace System;
void main1();
void main2();
void main()
{
   main1();
   Console::WriteLine();
   main2();
}

void PrintValues( Array^ myArr );
void main1()
{
   // Creates and initializes two new Array instances.
   Array^ mySourceArray = Array::CreateInstance( String::typeid, 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::typeid, 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 instance 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 );
}

void PrintValues( Array^ myArr )
{
   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}", myEnumerator->Current );
   }

   Console::WriteLine();
}


/*
 This code produces the following output.
 
  The target Array instance 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
 */
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
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes two new Arrays.
        Dim mySourceArray As Array = Array.CreateInstance(GetType(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)
        Dim myTargetArray As Array = Array.CreateInstance(GetType(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, " "c)
        
        ' Copies the source Array to the target Array, starting at index 6.
        mySourceArray.CopyTo(myTargetArray, 6)
        
        ' Displays the values of the Array.
        PrintValues(myTargetArray, " "c)
    End Sub    
    
    Public Shared Sub PrintValues(myArr As Array, mySeparator As Char)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' 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

다음 코드 예제에서는 복사 하는 방법을 보여 줍니다.는 ArrayArray 0이 아닌을 사용 하 여 합니다.The following code example shows how to copy an Array to another Array with a nonzero lower bound. 전체 소스 Array 대상에서 기존 요소를 덮어쓰지는 빈 요소를 포함 하 여 복사 됩니다 Array합니다.Note that the entire source Array is copied, including empty elements that overwrite existing elements in the target Array.

void PrintIndexAndValues( Array^ myArray );
void main2()
{
   // Creates and initializes the source Array.
   Array^ myArrayZero = Array::CreateInstance( String::typeid, 3 );
   myArrayZero->SetValue( "zero", 0 );
   myArrayZero->SetValue( "one", 1 );

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

   // Creates and initializes the target Array.
   array<int>^myArrLen = {4};
   array<int>^myArrLow = {2};
   Array^ myArrayTwo = Array::CreateInstance( String::typeid, 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 lowbound=2 contains:" );
   PrintIndexAndValues( myArrayTwo );

   // Copy from the array with lowbound=0 to the array with lowbound=2.
   myArrayZero->CopyTo( myArrayTwo, 3 );

   // Displays the modified target Array.
   Console::WriteLine(  "\nAfter copying at relative index 1:" );
   PrintIndexAndValues( myArrayTwo );
}

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 lowbound=0 contains:
     [0]:    zero
     [1]:    one
     [2]:    
 The array with lowbound=2 contains:
     [2]:    two
     [3]:    three
     [4]:    four
     [5]:    five
 
 After copying at relative index 1:
     [2]:    two
     [3]:    zero
     [4]:    one
     [5]:
 */
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 Shared Sub Main()
        ' Creates and initializes the source Array.
        Dim myArrayZero As Array = Array.CreateInstance(GetType(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.
        Dim myArrLen As Integer() = {4}
        Dim myArrLow As Integer() = {2}
        Dim myArrayTwo As Array = Array.CreateInstance(GetType(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(ControlChars.Cr + "After copying to the target array from " _
           + "index 3:")
        PrintIndexAndValues(myArrayTwo)
    End Sub
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' 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]: 

설명

이 메서드는 현재 인스턴스의 배열 요소를 모두 복사 합니다 array 대상 배열 인덱스부터 index합니다.This method copies all the elements of the current array instance to the array destination array, starting at index index. array 대상 배열 해야 이미 있는 된 차원이 및 요소 복사 된 요소에 맞게 충분 한 수 있어야 합니다.The array destination array must already have been dimensioned and must have a sufficient number of elements to accommodate the copied elements. 그렇지 않으면 메서드에서 예외가 throw됩니다.Otherwise, the method throws an exception.

이 메서드는 System.Collections.ICollection 인터페이스입니다.This method supports the System.Collections.ICollection interface. 구현 하는 경우 System.Collections.ICollection 가 사용 하 여 명시적으로 필요한 Copy 추가 간접 참조를 방지 하려면.If implementing System.Collections.ICollection is not explicitly required, use Copy to avoid an extra indirection.

이 메서드는 상태를 복사 하는 동안 예외를 throw 하는 경우 array 정의 되지 않습니다.If this method throws an exception while copying, the state of array is undefined.

이 메서드는 O (n) 작업, 여기서 nLength합니다. 단순 복사만 수행합니다.This method is an O(n) operation, where n is Length.It performs a shallow copy only.

추가 정보

적용 대상